Reputation: 255
If I have a UIPopoverController (in .h file) and alloc init it multiple times in the same .m file. do I need to release it once to multiple times?
Upvotes: 0
Views: 107
Reputation: 86651
You don't alloc an object multiple times. You create it by allocing it once. Each time you send alloc to a class, you create another new instance. Since you alloced all these instances, (see the memory management rules), you must release or autorelease them all.
Upvotes: 0
Reputation: 138051
Reference count is at stake here.
Here's the rule: objects die when no one owns them anymore. If you lose the reference to it without releasing it, you leak.
There are two common ways to gain ownership over an object:
alloc
method)retain
on itAnd, as well, there are two common ways to relinquish ownership over an object:
release
on itautorelease
on itSo each time you allocate an object, you are responsible for releasing it once you're done with it. This probably means you only have to release it once, even though you can create it through several code paths. However, you must ensure you release it if you're going to overwrite the variable with a new object.
Upvotes: 4