Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

difference between drain, release,dealloc and retain in Objective-C/

Hi i want to know the difference between drain, release,dealloc and retain in Objective-C.

like image 889
suse Avatar asked Nov 02 '09 05:11

suse


People also ask

What are the methods of retain and release 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.

What is the use of DEALLOC in Objective C?

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.

What is the difference between init and DEALLOC?

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.

Are init and DEALLOC risky in Objective-C?

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.


1 Answers

  • retain increase the reference count on an object
  • release decreases the reference on an object
  • drain is used in place of release on ONLY for NSAutoreleasePool objects due to some arcana related to the Objective C garbage collection
  • dealloc 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.

like image 160
Louis Gerbarg Avatar answered Sep 17 '22 18:09

Louis Gerbarg