Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

amit-battan

macrumors newbie
Original poster
Sep 10, 2009
16
0
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

Code:
-(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
 
You are releasing an object when you should not. You did not create tempArrIniId via alloc/init or copy so you should not release it. Go at read the Memory Management document (especially the page I linked to).
 
Code:
int s;
if([tempArrIniId containsObject:selectedInitiateId]){
     s=[tempArrIniId indexOfObject:selectedInitiateId];
}
[tempArrIniId release];
NSString *selStatusId = [NSString stringWithString:[[eventInitiationArray objectAtIndex:s] objectForKey:@"status_id"]];
This section sets the value of s if the id exist in the array and only in the array. You then use the int s in the next statement out side of the check. What happens if s is not set. If the value is not in the array s is just a nil value, therefore you will not be able to get the value for
Code:
[eventInitiationArray objectAtIndex:s]
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.