-1

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.

stefos
  • 1,235
  • 1
  • 10
  • 18
  • What do you want to test exactly? If you want to make a class instance a zombie just create a strong property in the class and assign self to it. If you want to detect if you are sending messages to a released weak reference or not just test for nil. – zaph Jun 24 '15 at 21:34
  • Why not just turn on NSZombie? http://stackoverflow.com/questions/535060/how-to-add-nsdebug-h-and-use-nszombie-in-iphone-sdk – Paulw11 Jun 24 '15 at 21:40
  • I want to create a case that will crash because of a dangling pointer so i can catch it in Zombies Instrument. But sending a message to deallocated object with weak reference does not crash. – stefos Jun 25 '15 at 10:44

1 Answers1

1

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.)

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • If the question is: "How is NSZombie implemented?" this article tells you: https://mikeash.com/pyblog/friday-qa-2011-05-20-the-inner-life-of-zombies.html – matt Jun 24 '15 at 22:25
  • Thanks for your answer. – stefos Jun 25 '15 at 10:50