-2

I have a small confusion in my application.

How can I check whether an object was released or not in iPhone?

Ravi Kumar Karunanithi
  • 2,151
  • 2
  • 19
  • 41
  • 6
    you wouldn't check for it, you would better manage the memory in a way that you know your object is released or not –  Sep 13 '11 at 10:01

3 Answers3

12

Sorry, but you're trying to solve the wrong problem.

If you follow some simple rules there is absolutely no need "find out" whether an object has been released or not; you will know.

The simple rules are:

  • If you alloc, copy or retain an object, then you are responsible for releasing it
  • Otherwise, you are not responsible for releasing it

Do not use retainCount. If the object has been deallocated (i.e., its retain count is zero), then you can't perform any operations on the object since it no longer exists! Also, even if it's currently one, what's to say that it's not in the autorelease pool and will be zero the next time you look?

Stephen Darlington
  • 51,577
  • 12
  • 107
  • 152
2

If object was released then you cannot access its properties.

You can use Profiler (NSZombies) to detect which objects were released and then accessed.

Nekto
  • 17,837
  • 1
  • 55
  • 65
2

any message to the object when zombies are enabled will suffice. if the program crashes because you messaged a zombie, then you know!

if you have enabled reference count tracking in instruments, then you can see each frame of each ref count event and find out where the invalid ref count offset has been introduced.

justin
  • 104,054
  • 14
  • 179
  • 226