mikeash / MAZeroingWeakRef

Zeroing weak references for retain/release Objective-C
Other
360 stars 57 forks source link

ZWR Runtime functions shouldn't always be used #9

Closed mlilback closed 12 years ago

mlilback commented 12 years ago

On Snow Leopard, MAZeroingWeakRef worked with every class I tried. On Lion, it doesn't work with subclasses of NSViewController, which do not support ZWRs since NSViewController overrides retain and/or release.

I'm not sure how to check to see if a class supports ZWR, but MAZWR should fallback to the old mechanism when the runtime version of ZWR won't work.

mikeash commented 12 years ago

It's possible to detect whether an object supports native ZWR using the allowsWeakReference message. See the weak_register_no_lock function here: http://opensource.apple.com/source/objc4/objc4-493.9/runtime/objc-weak.mm

Unfortunately, this is a private API and so probably shouldn't be relied upon. I think that the best approach here would be to write a separate little program that goes through all of the classes in the frameworks and fetches a list of ones that don't allow ZWR. That list could then be baked into MAZWR and used to decide whether to use native weak references or not. The list will only shrink with time, so it should be safe to do this.

mikeash commented 12 years ago

This is now done, using the above plan with the twist of using a table of SHA1 hashes instead of a table of class names to avoid falling afoul of app reviewers and private API.