views:

68

answers:

3

hi all! I wish best understand the difference between dealloc and release function.... example... I have my class derived from NSObject calle MyClass in my code, to use this class, I create an instance of MyClass..

// initialization
MyClass* test = [[MyClass alloc] init];

//do some stuff....

// release??
[ test release];

is right?? and the dealloc??? needs to be used in sequency or one overwrite the other??

+3  A: 

Have you seen this question ? http://stackoverflow.com/questions/559295/difference-between-release-and-dealloc-in-objective-c

Thanks

Pierre Valade
+1  A: 

As long as that's the end of test's life, you're correct. Dealloc of test will automatically happen as a function of your [ test release] statement.

umop
+2  A: 

dealloc is automatically called when retainCount is == 0. Each time you call [test release] the retainCount is decreased by one.

In your example everything is fine, since you have alloc test (retain count +1) and then release (retain count 0). Dealloc will be automatically called

Noya
release is done on instance of a class and not on a class :)
willcodejavaforfood
thanks but... the retainCount how works??example in c++ if I wrote`MyClass* test = new MyClass();``MyClass* test2 = new MyClass();`test and test2 are 2 instances completely separated.. I can release one and the other mantain for more time... in objective-c??if I write `MyClass* test = [[MyClass alloc] init];` and `MyClass* test2 = [[MyClass alloc] init];`the retainCount is 2? but retainCount is static from the class not from the instance
ghiboz
in your example you have two instances, each one with a retain count +1
Noya
ok, but can you write me an example where you have retainCount = 2 and what is the usage of this thing??thanks very much noya!p.s. ho visto che sei italiano!!!! :)
ghiboz