Fix memory leaks in vipsGetPoint and vipsImageGetAsString#510
Merged
davidbyttow merged 2 commits intomasterfrom Mar 6, 2026
Merged
Fix memory leaks in vipsGetPoint and vipsImageGetAsString#510davidbyttow merged 2 commits intomasterfrom
davidbyttow merged 2 commits intomasterfrom
Conversation
The deferred g_free captured a nil pointer (evaluated at defer time, before the C call populated it), so the C-allocated array was never freed. Additionally, the returned Go slice was backed by C memory that could be freed or reused. Fix: copy C data into a Go-owned slice, then free the C allocation immediately. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
The deferred freeCString captured a nil pointer (evaluated at defer time, before the C call populated it), so the g_free-allocated string from vips_image_get_as_string was never freed. Fix: wrap in a closure so the variable is captured by reference and evaluated when the deferred function actually executes. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
g_freecaptured a nil pointer before the C call populated it, so the C-allocated array was never freed. The returned Go slice was also backed by C memory. Fixed by copying into a Go-owned slice and freeing immediately.g_free-allocated string fromvips_image_get_as_stringwas never freed. Fixed by wrapping the defer in a closure.Both bugs stem from Go's
defer f(x)evaluatingxat the defer statement, not at function exit.Test plan
TestGetPoint_RepeatedCalls- calls GetPoint 10 times, retains all results, verifies correctnessTestGetAsString_RoundTrip- sets a string field, reads it back with GetAsString🤖 Generated with Claude Code
Note
Fix memory leaks by returning Go-owned slices from
vips.GetPointand freeing C strings invips.ImageGetAsStringAdjust
vips.GetPointto copy C buffers into Go slices and free the C memory, and updatevips.ImageGetAsStringto defer freeing the C string with the final pointer value. Add tests for repeatedGetPoint(10,10)calls and string metadata round-trip in image_test.go. Modify implementations in operations.go.📍Where to Start
Start with
vipsGetPointandvipsImageGetAsStringin operations.go.Macroscope summarized 381b362.