Friday, August 8th, 2008

Over on Apple’s Cocoa-dev mailing list, Peter Lewis asked if there was a way to release a CGImageRef. It turns out that this is trivial. In a program which isn’t garbage collected, you can just cast any CFTypeRef to an id and autorelease it: [(id)myCarbonObject autorelease].

If your app might be run under the garbage collector, though, that’s not good enough. You need to run NSMakeCollectable on the CFTypeRef: [NSMakeCollectable(myCarbonObject) autorelease].

Fortunately, even though NSMakeCollectable is new in OS X 10.5 Leopard, it’s an inline function and is therefore usable in apps targeting 10.4 Tiger.

I was excited to learn this because I had to jump through some hoops to release Carbon objects in the CFNetwork code I wrote recently. I can now happily just autorelease it.

Comments are currently closed for this post.