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 whole books full of them! Volumes II and III are now out! They're available as ePub, PDF, print, and on iBooks and Kindle. Click here for more information.


It looks like smart pointers in C++ :)
Why don't you use message forwarding mechanism instead of [[ref target] message]?
Is it good if I ask you about your code in comments?
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.

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.

The Answer to the Ultimate Question of Life, the Universe, and Everything?
Formatting: <i> <b> <blockquote> <code>.
NOTE: Due to an increase in spam, URLs are forbidden! Please provide search terms or fragment your URLs so they don't look like URLs.
Code syntax highlighting thanks to Pygments.
Hosted at DigitalOcean.