Objective-C weak reference zombie

611 views Asked by At

I'm trying to create a zombie object to detect sending messages to a deallocated object.

Say i have a strong property object A with a weak reference to object B. When B is deallocated my weak reference becomes nil but calling a method e.g [obj1.obj2 somemethod] simply returns nil not causing a crash.

Is there a way to test zombies using weak references? I can only crash using unsafe_unretained.

1

There are 1 answers

1
matt On BEST ANSWER

Is there a way to test zombies using weak references

(I take it that by "zombies" you actually mean dangling pointers...)

Not directly, no. The whole point of ARC-weak references is that they prevent dangling pointers. (As you rightly say, they safely replace the potential dangling pointer with nil — and there's no penalty for sending a message to nil.)

The reason there can be dangling pointer crashes in real life is that most of Cocoa does not use ARC. (As you rightly say, it uses unsafe_unretained.)