[Openmcl-devel] Retain/Release and the ObjC Bridge

Brent Fulgham bfulg at pacbell.net
Tue Aug 28 20:59:33 PDT 2007


Gary,

The new Cocoa bridge is great to work with, but I'm a little confused  
as to the semantics of releasing data from the bridge.  Should we  
treat the Lisp objects returned by API calls just as we would in C  
(i.e., we must manually call release in many cases), or does OpenMCL  
ever clean up when objects go out of scope?

Example:

  (let* ((Name         (%make-nsstring (native-translated-namestring  
File)))
         (url          (#_CFURLCreateWithFileSystemPath (%null-ptr)  
Name #$kCFURLPOSIXPathStyle #$NO))
         (imageSrcRef  (#_CGImageSourceCreateWithURL url (%null-ptr)))
         (imageRef     (#_CGImageSourceCreateImageAtIndex imageSrcRef  
0 (%null-ptr)))
         (uprightImage (#/imageWithCGImage: (@class c-i-image)  
imageRef)))
    (if (%null-ptr-p uprightImage)
        (error "cannot decode texture file \"~A\"" (native-translated- 
namestring Filename)))
      (let* ( ;; Flip image (if necessary)
             (image (if Flip-Vertical (#/imageByApplyingTransform:  
uprightImage transform) uprightImage))
             (rect (#/extent image))
             (depth (#_CGImageGetBitsPerPixel imageRef))
             (width (#_CGImageGetWidth imageRef))
             (height (#_CGImageGetHeight imageRef))
             (bytesPerRow (#_CGImageGetBytesPerRow imageRef))
             (bytesPerPixel (truncate depth 8))
             (bitmap (#/initWithCIImage: (#/alloc (@class n-s-bitmap- 
image-rep)) image))
             (Codec (#/bitmapFormat bitmap))
             (samples-per-pixel (#/samplesPerPixel bitmap)))
         (when Verbose
           (format t "~%-loading image ~A size: ~A x ~A, depth: ~A"  
Filename width height depth)
           (format t "---> bytes-per-pixel/samples-per-pixel: ~A/~A~%"  
bytesPerPixel bytesPerPixel) ;samples-per-pixel)
           (format t "---> bitmap format: ~A~%" (#/bitmapFormat  
bitmap)))

         ;; FIXME:  Need Core Image filters to change depth if required
         (when Forced-Depth (setq depth Forced-Depth)) ; Currently we  
don't do anything with this ...
         (#_CFRelease image)
         (#_CFRelease imageRef)
         (#_CFRelease imageSrcRef))))))

This code seems to work fine for me, but I'm wondering if the  
CFRelease calls I am making are always necessary.  Or am I perhaps  
missing some (perhaps the URL should be released, too?)

And to add further confusion, will the answer to the above change when  
Objective C 2.0 is used? -- I would imagine it must if garbage  
collection is used.

Thanks,

-Brent
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.clozure.com/pipermail/openmcl-devel/attachments/20070828/7958cff3/attachment.htm>


More information about the Openmcl-devel mailing list