views:

97

answers:

6

Hi, i'm writing a program which do something like that:

- (void)viewDidLoad {
    [super viewDidLoad]; propertyList = [[NSMutableDictionary alloc] init];
    for (SensorData* _data in _res) {
        AttributeLine* newLine=[[AttributeLine alloc] init:idx];
        [propertyList setObject:newLine forKey:[_data textValue]];
        [newLine release];
    }
}

- (void)dealloc {
    [propertyList release];
    [super dealloc];
}

It's not complete the code, but there is the logic of what i do. The problem is: when the program arrives to the [propertyList release] instruction, fail with an EXEC_BAD_ADDRESS. If i comment that instruction everything goes fine, but i have memory leaks. Any idea ?

[Edit by bbum: marco said below that his dealloc is correct and the code is still crashing]

+1  A: 

In -dealloc, you do [super dealloc] not [super release]. And [super dealloc] should be the last line of the -dealloc method.

- (void)dealloc {
    [propertyList release];
    [super dealloc];
}
Kurbz
A: 

You should move [propertyList release] to the first line in dealloc, and then change [super release] to [super dealloc]. You don't want to release super, you want to call dealloc on it.

pkananen
A: 

I would recommend initializing the NSMutableDictionary either in the initWithNibName:bundle: or initWithCoder: (or both) of your viewcontroller since viewDidLoad can get called more than once. The second time that would occur, you'd leak. Also, your dealloc method is wrong. It should be

-(void) dealloc {
     [propertyList release];
     [super dealloc];
}

The EXC_BAD_ADDRESS is probably caused by your bad dealloc method.

Elfred
A: 

Ops, sorry. My code is already correct in dealloc method:

- (void)dealloc {
    [propertyList release];
    [super dealloc];
}

Sorry i wrote the code fast without the copy and paste ! So the problem still remain :(

Marco
Edit your question, no one will figure out that a random answer is a fix to the question.
bbum
+1  A: 

If your code really is correct as you said in an answer (and I edited into your question), then there is something else going on.

First, post the backtrace of the crash and any console messages that happen prior to the crash.

Secondly, turn on zombies (use the Zombie Detection mode in Instruments's Allocations instrument) and see if you get some additional clues.

Finally, might you be crashing in AttributeLine's -dealloc method? As a result of deallocating the propertyList dictionary, those objects are likely to be deallocated, too.

bbum
A: 

Hi, i solved the problem: everything is connected with the AttributeLine's -dealloc method. My AttributeLine object is composed by three labels and inside the -dealloc that i wrote before, for all those labels i called their -dealloc method. Now instead, i call for every label its release method and eveything seem to go now. No program interruption and no memory leaks !

Thank you for the support guy !

Marco