views:

42

answers:

1

Hi All

I am using following code for showing a next view(tableview) in Iphone app.

It sometime works and some time app crashes without any exception log

NSLog(@"Ok"); is log everytime 50% attempts application crashes

-(IBAction)statusInitiationButAction:(id)sender{
    @try {
        NSArray *tempArrIniId = [eventInitiationArray valueForKey:@"act_id"];
        int s;
        if([tempArrIniId containsObject:selectedInitiateId]){
            s=[tempArrIniId indexOfObject:selectedInitiateId];
        }
        [tempArrIniId release];
        NSString *selStatusId = [NSString stringWithString:[[eventInitiationArray objectAtIndex:s] objectForKey:@"status_id"]];

        for (int i=0; i<[statusInitiationArray count]; i++) {
            id statusDict = [statusInitiationArray objectAtIndex:i];
            if ([selStatusId intValue] == [[statusDict objectForKey:@"id"] intValue]) {
                [statusDict setValue:@"1" forKey:@"selected"];
                [statusInitiationArray replaceObjectAtIndex:i withObject:statusDict];
            }else {
                [statusDict setValue:@"0" forKey:@"selected"];
                [statusInitiationArray replaceObjectAtIndex:i withObject:statusDict];
            }
        }
        NSLog(@"statusInitiationTable...%@",statusInitiationArray);
        [statusInitiationTable.tableView reloadData];
        [[self navigationController] pushViewController:statusInitiationTable animated:YES];
        NSLog(@"ok");
    }@catch (NSException * e) {
        NSLog(@"statusInitiationButAction....%@",e);
    }
}

Can anybody guide me about the problem.

Thanks

Amit Battan

+4  A: 

You should not be doing this:

 [tempArrIniId release];

Because in this line...

NSArray *tempArrIniId = [eventInitiationArray valueForKey:@"act_id"];

...you are not creating the tempArrIniId you are merely obtaining a reference to it. Therefore, you did not retain it and have no need to release it.

You are getting an intermittent crash because your over releasing the object pointed to by tempArrIniId while that object is still a member of the eventInitiationArray. When the array tries to access the object or even count itself, it crashes because there is not an object where it expects one to be. That crash can happen anywhere in the app where the 'eventInitiationArray' is used.

Overzealous releasing causes more problems than it prevents. When in doubt, don't release. It's trivial to find memory leaks with the analysis tools if you don't release something you should have and it's trivial to fix it.

It's a lot harder to track down a crash caused by over releasing an object held by other objects such as arrays because the subsequent crash can occur far from where the over release occurred.

TechZen