Fitzy
Fitzy

Reputation: 1889

object still exists after setting to nil

I am using ARC. I have a method that runs at the end of a game I have written which should clear up memory. There are a series of objects in an NSMutableArray, which i remove using removeObject:. I then set these objects to nil. However, using NSLog on these objects shows that they still exist. Why does setting them to nil not remove them from memory?

Upvotes: 0

Views: 253

Answers (1)

sergio
sergio

Reputation: 69027

In ARC (automatic reference counting), setting a reference to an object to nil means two different things depending on the kind of reference you are nil-ing:

  1. If it is a strong reference, then nil-ing it means decreasing the reference count of the referenced object;

  2. if it is a weak reference, nil-ing it does nothing.

Thus, nil-ing can lead to different outcomes. Specifically, it is only when the reference count goes to zero that the object is deallocated. This would correspond to a case where no other object in the system is owning the first one (which means holding a strong reference to it).

So, in your case there could be either some other objects keeping a strong reference to the objects you try to nil; or, you might be nil-ing a weak reference. If you show some code, it may become clearer.

Upvotes: 1

Related Questions