mikeash.com: just this guy, you know?

Posted at 2010-07-16 20:19 | RSS feed (Full text feed) | Blog Index
Next article: Friday Q&A 2010-07-30: Zeroing Weak References to CoreFoundation Objects
Previous article: Friday Q&A 2010-07-16: Zeroing Weak References in Objective-C
Tags: code hack objectivec sourcecode
Introducing MAZeroingWeakRef
by Mike Ash  

I'm extremely excited to announce a new library for Cocoa and Cocoa Touch development: MAZeroingWeakRef. In short, it's a library which allows zeroing weak references to be used in retain/release Cocoa code. This has all sorts of uses and should make retain/release coding less painful. While I discussed this in detail in my Friday Q&A post this week, I also want to make a separate announcement for people who don't want to read through all of the horrible details.

A zeroing weak reference is a reference to an object which does not prevent that object from being destroyed (in other words, it's not retained), and which automatically becomes nil once the object is destroyed. To use MAZeroingWeakRef, you simply create one using -initWithTarget::

    MAZeroingWeakRef *ref = [[MAZeroingWeakRef alloc] initWithTarget: object];
You can access the object at any time using the -target method:
    NSLog(@"Target is %@", [ref target]);
As long as the object continues to exist, -target will return it. Once it is destroyed, -target will return nil. -target returns a reference that has been retained and autoreleased, ensuring that the returned object will remain valid as you use it even if the last strong reference to it has been released in another thread while you work.

You can get MAZeroingWeakRef from my public subversion repository:

    svn co http://mikeash.com/svn/ZeroingWeakRef
It's released under a BSD license, so you can use it in your commercial applications with acknowledgement.

MAZeroingWeakRef should compile and run on any OS with the "modern" runtime APIs, which is basically 10.5 and up, and any iOS version. Blocks support is needed to enable the cleanup block functionality (which allows running arbitrary code when the reference is destroyed) but everything else will work without it.

If you use MAZeroingWeakRef on iOS, you may want to set COREFOUNDATION_HACK_LEVEL to 0 in MAZeroingWeakRef.m. This prevents MAZeroingWeakRef from being able to target CoreFoundation bridged objects, but also avoids the use of private API, which Apple can sometimes be unfriendly about. This is not a big disadvantage, as it's extremely rare to have weak references to CF objects.

I hope that this library will prove useful!

Did you enjoy this article? I'm selling a whole book full of them. It's available for iBooks and Kindle, plus a direct download in PDF and ePub format. It's also available in paper for the old-fashioned. Click here for more information.


Ilya at 2010-07-17 04:16:34:
It looks like smart pointers in C++ :)
Why don't you use message forwarding mechanism instead of [[ref target] message]?

Ilya at 2010-07-17 05:00:27:
Is it good if I ask you about your code in comments?

mikeash at 2010-07-17 13:51:59:
Somebody else asked me why I didn't use a proxy too, in my other post. See here:


And it's fine if you ask about code, but since more discussion is happening in that other post, you may want to ask over there instead.

192.168.l.254 at 2016-04-30 16:29:39:
happy to join your community, found a lot of useful information!

Comments RSS feed for this page

Add your thoughts, post a comment:

Spam and off-topic posts will be deleted without notice. Culprits may be publicly humiliated at my sole discretion.

Web site:
Formatting: <i> <b> <blockquote> <code>. URLs are automatically hyperlinked.
Code syntax highlighting thanks to Pygments.
Hosted at DigitalOcean.