BaSha
BaSha

Reputation: 2406

Is memory released when reference count set to zero?

I have found that [object_name retain] increases that object reference count by 1 and [object_name release] decreases that object reference count by 1. [pool drain] applies release on all the objects it refers to.

Now I'm confused. What happens to an object when there is no reference to it? release and drain just decrementing reference count so does memory is actually released on zero reference count or do I still have an object with a zero reference count?

I also found if here

[pool drain] // makes my Object str3 reference count Zero

NSLog(@"%li",[str3 length]); // it works, object may be not destroyed yet!

// other code here

But

[pool drain]

// other code here

NSLog(@"%li",[str3 length]); // not working, may be it is destroyed now!

Upvotes: 1

Views: 912

Answers (2)

Catfish_Man
Catfish_Man

Reputation: 41821

Objects are deallocated when there are no strong references to then (i.e. when release lowers their retain count to 0).

Remember, though, that the -retainCount method is useless and you shouldn't call it.

Upvotes: 5

Paramasivan Samuttiram
Paramasivan Samuttiram

Reputation: 3738

I think the object will be destroyed after a zero retain count, and cannot be used anymore. Please refer here for more.

Upvotes: 0

Related Questions