On Fri, 24 Jul 2026 11:47:05 GMT, Jose Pereda <[email protected]> wrote:
> This PR releases the NSImage that `SetNSPasteboardItemValueForUtf()` in
> GlassPasteboard.m obtains from `getImage()` in GlassPixels.m. Once the pixel
> data is used, such image is not longer needed and has to be released,
> preventing a memory leak.
>
> Similar operations in GlassCursor.m and GlassMenu.m that use the same NSImage
> allocation mechanism, did already release the image in this very same way, so
> this PR just adds the missing release call to GlassPasteboard.m.
>
> There are no tests included, since this is a pure native objective-c memory
> leak (retained NSImages), and it can't be tracked down from the Java side,
> but I have tested manually with Instruments and the Leaks template, with this
> code snippet in MacPasteboardTest:
>
>
> private void pushImage() {
> int size = 512;
> ByteBuffer buffer = ByteBuffer.allocate(size * size * 4);
> Pixels pixels = Application.GetApplication().createPixels(size, size,
> buffer);
> macPasteboardShim.pushMacPasteboard(new
> HashMap<>(Map.of(Clipboard.RAW_IMAGE_TYPE, pixels)));
> }
>
> calling it repeatedly for some time.
>
> Before the fix:
>
> <img width="1236" height="168" alt="image"
> src="https://github.com/user-attachments/assets/62cf81a1-fd77-46a4-bce6-8575109d2dd2"
> />
>
> as shown, the leaks are in `getImage` from libglass.dylib, that allocates a
> `NSImage` with a `CGImage` (including same amount of internal data,
> providers, arrays, snapshots objects).
>
> After the fix the leaks are gone:
>
> <img width="1049" height="134" alt="image"
> src="https://github.com/user-attachments/assets/e9049674-3f4d-42ab-83ad-66601ea337a1"
> />
>
> ---------
> - [X] I confirm that I make this contribution in accordance with the [OpenJDK
> Interim AI Policy](https://openjdk.org/legal/ai).
This looks like a correct fix to me. `NSImage::TIFFRepresentation` returns a
new image and doesn't take ownership of the `NSImage`, so releasing it is the
right thing to do.
-------------
Marked as reviewed by kcr (Lead).
PR Review: https://git.openjdk.org/jfx/pull/2222#pullrequestreview-4832424190