Hi i want to know the difference between drain, release,dealloc and retain in Objective-C.
Objective-C uses two methods retain and release. In Objective-C each object has an internal counter that is used to keep track of all references used by the objects or object has. [object retain] increments the counter by 1 and [object release] decrements the counter by 1. When counter reaches to zero, dealloc is then called.
In Objective-C each object has an internal counter that is used to keep track of all references used by the objects or object has. [object retain] increments the counter by 1 and [object release] decrements the counter by 1. When counter reaches to zero, dealloc is then called.
At the very beginning of dealloc. That’s because in those two places, the object has self-consistency. In init, all the ivars have been set up. In dealloc, none of the ivars have yet been destroyed. But you still have to exercise caution and recognize where you are in the object’s lifetime.
But there are a couple of places where doing so is risky in Objective-C: init and dealloc. This post is part of the Code Smells in Objective-C series.
retain
increase the reference count on an objectrelease
decreases the reference on an objectdrain
is used in place of release on ONLY for NSAutoreleasePool objects due to some arcana related to the Objective C garbage collectiondealloc
is called by the system once the retainCount of an object hits 0. It is where you clean up various things your object has (like a deconstructor or finalizer). You should NEVER call it directly, except for calling [super dealloc]
at the end of your dealloc
routines.You really should just read through Apple's memory management documentation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With