Skip to content

Fix memory leaks in vipsGetPoint and vipsImageGetAsString#510

Merged
davidbyttow merged 2 commits intomasterfrom
fix/memory-leaks
Mar 6, 2026
Merged

Fix memory leaks in vipsGetPoint and vipsImageGetAsString#510
davidbyttow merged 2 commits intomasterfrom
fix/memory-leaks

Conversation

@davidbyttow
Copy link
Owner

@davidbyttow davidbyttow commented Mar 5, 2026

Summary

  • vipsGetPoint: The deferred g_free captured 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.
  • vipsImageGetAsString: Same defer-captures-nil pattern. The g_free-allocated string from vips_image_get_as_string was never freed. Fixed by wrapping the defer in a closure.

Both bugs stem from Go's defer f(x) evaluating x at the defer statement, not at function exit.

Test plan

  • TestGetPoint_RepeatedCalls - calls GetPoint 10 times, retains all results, verifies correctness
  • TestGetAsString_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.GetPoint and freeing C strings in vips.ImageGetAsString

Adjust vips.GetPoint to copy C buffers into Go slices and free the C memory, and update vips.ImageGetAsString to defer freeing the C string with the final pointer value. Add tests for repeated GetPoint(10,10) calls and string metadata round-trip in image_test.go. Modify implementations in operations.go.

📍Where to Start

Start with vipsGetPoint and vipsImageGetAsString in operations.go.

Macroscope summarized 381b362.

davidbyttow and others added 2 commits March 5, 2026 15:52
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>
@coveralls
Copy link

coveralls commented Mar 5, 2026

Coverage Status

coverage: 36.732% (+0.4%) from 36.365%
when pulling 381b362 on fix/memory-leaks
into 12bbea8 on master.

@davidbyttow davidbyttow merged commit b7aa4f1 into master Mar 6, 2026
3 checks passed
@davidbyttow davidbyttow deleted the fix/memory-leaks branch March 6, 2026 00:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants