What could be causing pictures to leak?

So I have this desktop app that uses GM(16)ImageMBS to load a rather big scan (> 10,000 x 6,000) and retrieve color patches and their histogram data from it. When this Image is loaded, I create a picture copy of it using GMImageMBS.CopyPicture and assign it to a private picture property of my test window. It will then be drawn to a Canvas and is being drawn on for a live update of the patch retrieval.

When all is done, user may load another GMImage and process can be repeated.

This is working nicely now, but when using Activity Monitor I saw the memory consumption of the app rise dramatically after each image processing. The JsonItem it builds as a result can be quite big, like > 80 MB, so some memory impact is to be expected. But not to climb to over 4 GB after just a few scans!

I used XojoInstruments to find the leak and, besides some minor short string leaks, found that the preview picture from the CopyPicture method is never released, causing the enormous memory appetite. What’s even more confusing: The picture is not referenced by anything according to Instruments. It simply will not get released. It was held in this private window picture property, and I tried to ObjC release it (crash for overrelease), CFRelease it (no change), Nil it before assigning a new value: No change at all! I tried to loop GMImageMBS.CopyPicture, thinking this could be the cause: No additional change in memory.

Any ideas what could be causing such a leak? @Christian_Schmitz in case this could be connected to GMImage somehow? The working image is similarly held in a private window property and replaced, without me seeing any leaks on it.

1 Like

Did you try to Close it (all three objects) ?

If you can tell me how to close a picture?
As for the window: Yes. No change in memory usage.

Well, if you have a memory leak, it should be easy for you to reproduce it in a small new project.

By building this little test project to reproduce it, you may discover whether that shows the leak or the whole thing is a logic error in your own project.

A quick test here doesn’t show a leak. e.g. a timer with this Action event:

dim geo as new GMGeometryMBS(10000,10000)
dim g as new GMImageMBS(geo, GMColorMBS.White)

dim p as Picture = g.CopyPicture

And if not, you’ll have a sample to submit in a case to Xojo so they can find what’s going on behind the scenes.

Maybe it involves a mix with JSON

That you, Christian. I just disabled some methods in my test app and found it is being zbarMBS that’s holding the picture in memory from a ZbarMBS.scan(picture) method. According to XojoInstruments, the zbarMBS objects are not destroyed, and they hold a reference to the original picture!

I thought it would be me holding the zbars in place when I put them into a listbox’ row tag, although that listbox is cleared. But I removed that, and I can still find the old zbars in memory after I loaded a different picture.
Could you please have look at it? Do you want me to send you the test project?

1 Like

Found it.
ZBarMBS has a wrong reference count when returned.

Thanks for reporting.
I’ll fix it for 23.0 final later today.

PS: There may be other memory leaks, you also see.


Fix confirmed, Christian!
Like so often: Thanks a lot for that outstanding fast fix!

Bild 16.01.23 um 15.36