views:

71

answers:

3

If i create a simple class like this

@interface table : NSObject {

    NSMutableArray *array;
}

@end

and in the Init method I call: array = [[NSMUtableArray alloc] init]; and in the dealloc method I call: [array release];

but I have a memory leek because the dealloc method is never called. I must call the dealloc method by myself?

thx, Alex

A: 

No. You should never directly call [self dealloc]. What is it that gives you the impression that -dealloc is never called?

EDIT: You should never need to call [table dealloc] either. The only time you should ever send a dealloc message is when overriding dealloc and calling [super dealloc].

David Foster
Because I run with the leek tools, and he said that this table leek, and I put a breakpoint in the dealloc method and it's never called...
alex
So then the main question is "when do you call [myTable release]?"
Seamus Campbell
A: 

When an object's retain count hits 0 it is marked by the run time for cleanup. When the run time cleans up the object and reclaims the memory, dealloc is called giving it a chance to clean any other object references to instance variables or the like. Does that help? If you want to see it called, put

NSLog(@"%s", _cmd);

or a break point at the beginning of your dealloc method and watch the debugger/console.

David Weiss
the program don't go in the dealloc method if I put a breakPoint. My class is a singleton, it can changed something?
alex
A: 

This all looks correct. Your instance of class table here, will have it's dealloc method called when it is completely released. So you only have a leak if the way you use your class causes the entire instance of table to leak.

But, of course, the same rules apply for any classes that uses your table class.

Squeegy