MJN
MJN

Reputation: 11

calling -retain multiple times on the same object

What happens when you call -retain on an object many times? Is it OK to just release it once at when you're done using it?

Upvotes: 1

Views: 188

Answers (1)

iandotkelly
iandotkelly

Reputation: 9124

Generally you need to release the object as many times as it is retained - this is why its called reference counting. The holders of pointers to your object call -retain to keep it in memory. It will only be deallocated once its been -release'd the correct number of times. Retaining it more than necessary is therefore keeping memory allocated beyond its useful life - and is called a memory leak. The Xcode Instruments tool has a memory leak analysis tool.

Here is a good description of Memory Management

I also agree with @Chris who comments above that this Answer describing how retain/release works is very good.

Upvotes: 6

Related Questions