[Openmcl-devel] Cocoa and memory management

Ron Garret ron at flownet.com
Thu Jul 4 22:53:23 PDT 2013

I'm writing some code that grabs a stream of images from a camera and displays them in a window.  The code looks roughly like this:

  (multiple-value-bind (v p) (make-heap-ivector ...)
    (read-image-from-camera-into v)
    (let ((img (make-instance ns:ns-image :with-data
                 (make-instance ns:ns-data :with-bytes p :length (length v)))))
      (display-image img)
      (dispose-heap-ivector v)
      (#/release img)

I'm trying to figure out if I have a memory leak because of not #/releasing the ns-data instance that I'm allocating, so I let this run for a while.  According to top, I seem to have a leak because the image size grows.  But when I stop the loop, close the listener it was running in, and call GC, the image size shrinks back down again.

It seems like there are two possibilities:

1.  I'm allocating memory in the current listener's autorelease pool, and it's getting reclaimed when I close the listener.  Except I'm not calling #/autorelease anywhere.

2.  TOP is lying, and I'm not really leaking at all, but CCL is playing some games with virtual memory that fools top into thinking the image is growing when it's really not.

Any hints from those wise in the ways of objc memory management would be much appreciated.


More information about the Openmcl-devel mailing list