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).

-------------

Commit messages:
 - Release NSImage to prevent memory leak

Changes: https://git.openjdk.org/jfx/pull/2222/files
  Webrev: https://webrevs.openjdk.org/?repo=jfx&pr=2222&range=00
  Issue: https://bugs.openjdk.org/browse/JDK-8388786
  Stats: 2 lines in 1 file changed: 1 ins; 0 del; 1 mod
  Patch: https://git.openjdk.org/jfx/pull/2222.diff
  Fetch: git fetch https://git.openjdk.org/jfx.git pull/2222/head:pull/2222

PR: https://git.openjdk.org/jfx/pull/2222

Reply via email to