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 pull request has now been integrated.

Changeset: f8e39e78
Author:    Jose Pereda <[email protected]>
URL:       
https://git.openjdk.org/jfx/commit/f8e39e78b742534bb37a097d5c7e34b687e62c24
Stats:     2 lines in 1 file changed: 1 ins; 0 del; 1 mod

8388786: [macOS] NSImage leak in GlassPasteboard.m when using images for dnd or 
clipboard operations

Reviewed-by: mfox, kcr

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

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

Reply via email to