9

I want to ask an general question about the objective C. When I write the program of the iPhone application, I always see a function called 'dealloc' in the .m. when will this method be called? do I need to put all the [release] in here good for the application? thank you very much.

// ------------------ updated content -------------------------

NSArray *arr;
NSString *str;
NSMutableArray *mutableArr;

// in the dealloc
// it should have to following
[arr release];
[str release];
[mutableArr release];

the function will be call 3 times?

4 Answers 4

15

The dealloc method is called on an object when it's retain count has reached zero. Retain counts are increased by one for each retain call, and reduced once for each release call. The autorelease schedules a future release call when the current NSAutoreleasePool is drained, typically at the end of an event cycle, but you can set up your own NSAutoreleasePools on memory intensive operations. (See the NSAutoreleasePool docs for details.)

What should you put into dealloc? You should put a release for each member object the object of that class retains.

A couple things make this easier. The nil object will quietly ignore any messages sent to it, so [foo release] when foo = nil is not a bug. However, releasing an object twice can cause serious issues. My (hardly unique) solution to this is to explicitly set whatever I just released to nil, whenever I release it. In fact, I put the nil assignment on the same line as the release so I can grep for "release" and find places I missed. Example:

@interface MyClass {
    Foo *foo;
    Bar *bar;
    NSInteger baz;
}
-(void)dealloc;
@end

@implementation MyClass
-(void)dealloc {
    [foo release]; foo = nil;
    [bar release]; bar = nil;
    [super dealloc];
}
@end

I'll assign nil to a variable even when that variable is about to go out of scope or the object is about to go away. Why? If another object of the same class is allocated in the same memory space after I've released this one, it guarantees there will be no dangling pointers the new object might accidentally use and make debugging a nightmare. (See also NSZombieEnabled for debugging help.)

Sign up to request clarification or add additional context in comments.

5 Comments

You can use commas to separate instructions on a line. e.g [foo release], foo = nil; etc.
Interesting. Is it any better?
Nope, just more confusing. Its the comma operator (a , b) which results in the result of the right expression (if you'd use the value somewhere, e.g. x = y, z;).
@Abizern: that's a bad idea. Use the semicolon, that's wqhat it's for.
It's pointless setting foo and bar to nil in dealloc since after [super dealloc] the object they are in is gone.
5

when will this method be called?

It's called when the reference count for that object becomes 0 because all its pointers have been released. The memory taken up by it is deallocated (freed); the object itself is destroyed.

do I need to put all the [release] in here good for the application?

Yes, release all the properties of the object that are still retained.

EDIT: in response to your updated question, dealloc in your custom object is only called once. It will then send these three messages:

[arr release];
[str release];
[mutableArr release];

To each of the three objects. They are entirely different objects, but if you only have one instance of each, then their reference counts all go down to 0 and their dealloc methods are called automatically.

4 Comments

Thank you for your reply. I would like to ask about the 'when'. If i have 3 objects have to release. Then one of the objects becomes 0. Will the method be called?
When you have 3 instances of the object in your code, and you do an [object1 release] on (let's say) the first object, you still have 2 instances left elsewhere, so it isn't called yet. You'll need to release all instances of the object in order for dealloc to be called.
@Mark: Are you asking for "3 references to one object" or "3 distinct objects"?
@Georg Fritzsche, please see the post, I have updated it. thank you.
1

As you surmised, it's called when an object is destroyed. The object should release everything it owns.

Comments

0

In Other words, Dealloc frees/destroys/releases the memory you allocated to your objects. Allocated objects to memory should be destroyed once not used to avoid memory leaks that might cause your application to crash.

ZaldzBugz

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.