REALBasic Hard Crash on p.DrawPicture and CreateCGImageFromPixMap

I’ve been writing a small app at the moment that manipulates images in REALBasic (now called XOJO) and came across a peculiar crash. Basically, I’m drawing images into a larger image and after loading 100 images and drawing them into other files, it ended up crashing. Nothing could be caught. I soon realised that it was a memory issue (reaching over 4 gigs).

There were 2 things that I was doing wrong, and a work around for the issue. First was that I was using the ListBox as a datastore of images. I did this because it’s easy to store images in the celltag. I also show a preview of the image in the listbox. I changed this implementation to just keep a reference to the absolute path of the file and then load it when it wants to be displayed. This cut the memory size down to 30MB after loading 100 images. The problem here though, was any changes to the listbox, it would refresh ALL cells, and thus, reading in all 100 images and drawing them would slow down the application considerably. I then changed it back to store the image in the celltag. Memory jumped up to 2GB, but the background painting of the images was instantaneous and the operation of the application was excellent.

The second was that when I was generating these collaged images, I was pulling the images out of the listbox, drawing them, and then nulling the image reference after it was saved to disk. This seemed to NOT clear the memory, and the consumption would keep climbing before it would crash (as per the error above). The way around it, was to use the absolute path when loading up the images to be laid out, and thus, the memory usage doesn’t keep climbing, but only fluctuates with an additional 100MB at most. It slows down the process slightly (as it is reading from disk and not memory), but keeps the application stable.