开发者

releasing a NSMutableArray is causing EXC_BAD_ACCESS error

so when i try to run the following code, i end up with a EXC_BAD_ACCESS error. it happens when i try to release a NSMutableArray retrievedAnalysisDataList. the array is a list of retrievedAnalysisData objects. if i try to either release the data list or if i set up the init with an autorelease, i get the same result. i'm kinda guessing it has something to do with the sorting section of the code since i don't have this issue with the retrievedAnalysisIDarray.

any ideas?

if (tempDict != NULL) 
    {
        NSMutableArray *retrievedAnalysisDataList = [[NSMutableArray alloc] init];
        NSMutableArray *retrievedAnalysisIDarray = [[NSMutableArray alloc] init];

        for (id key in tempDict)
        {
            retrievedAnalysisData = [[RetrievedAnalysisData alloc] init];

            retrievedAnalysisData.createDate = [[tempDict objectForKey:key] objectForKey:@"createdate"];
            retrievedAnalysisData.ID = [[tempDict objectForKey:key] objectForKey:@"id"];
            retrievedAnalysisData.mode = [[tempDict objectForKey:key] objectForKey:@"mode"];
            retrievedAnalysisData.name = [[tempDict objectForKey:key] objectForKey:@"name"];
            retrievedAnalysisData.numZones = [[tempDict objectForKey:key] objectForKey:@"numzones"];
            retrievedAnalysisData.srcImg = [[tempDict objectForKey:key] objectForKey:@"srcimg"];
            retrievedAnalysisData.type = [[tempDict obj开发者_JAVA百科ectForKey:key] objectForKey:@"type"];

            //NSLog(@"\n createDate: %@ \n id: %@ \n mode: %@ \n name: %@ \n numzone: %@ \n srcimg: %@ \n type: %@", retrievedAnalysisData.createDate, retrievedAnalysisData.ID, retrievedAnalysisData.mode, retrievedAnalysisData.name, retrievedAnalysisData.numZones, retrievedAnalysisData.srcImg, retrievedAnalysisData.type);

            [retrievedAnalysisDataList addObject:retrievedAnalysisData];
            [retrievedAnalysisData release];
        }

        NSSortDescriptor *sortDescriptor;
        sortDescriptor = [[[NSSortDescriptor alloc] initWithKey:@"createDate" ascending:YES] autorelease];
        NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
        NSArray *sortedRetrievedAnalysisDataList;
        sortedRetrievedAnalysisDataList = [retrievedAnalysisDataList sortedArrayUsingDescriptors:sortDescriptors];

        int count = [sortedRetrievedAnalysisDataList count];

        for (int i = 0; i < count; i++) {
            retrievedAnalysisData = [[RetrievedAnalysisData alloc] init];
            retrievedAnalysisData = [sortedRetrievedAnalysisDataList objectAtIndex:i];

            [retrievedAnalysisIDarray addObject:retrievedAnalysisData.ID];
            [retrievedAnalysisData release];
        }

        dataCenter.sortedRetrievedAnalysisDataList = sortedRetrievedAnalysisDataList;
        dataCenter.retrievedAnalysisIDarray = retrievedAnalysisIDarray;

        [retrievedAnalysisIDarray release];
        [retrievedAnalysisDataList release];

        dataCenter.isRetrieve = [NSNumber numberWithInt:1];

        [activityIndicator stopAnimating];
        [picker reloadAllComponents];

        picker.hidden = FALSE;
        pickerToolBar.hidden = FALSE;
        toolBar.hidden = TRUE;
        innerCircle.hidden = TRUE;
        outerCircle.hidden = TRUE;
        trackLabel.hidden = TRUE;
        displayGPSLabel.hidden = TRUE;

    }


Your problem lies in this section of code:

        retrievedAnalysisData = [[RetrievedAnalysisData alloc] init];
        retrievedAnalysisData = [sortedRetrievedAnalysisDataList objectAtIndex:i];

        [retrievedAnalysisIDarray addObject:retrievedAnalysisData.ID];
        [retrievedAnalysisData release];

The first line allocates a new RetrievedAnalysisData, but then the second throws that away (leaking it) and places an object fetched from the array in the retrievedAnalysisData variable instead. You don't own this object fetched from the array, and you don't take ownership by calling retain. So the release on the fourth line is incorrect, releasing an object that you do not own.

Then when you release your NSMutableArray, it tries to release the object again and you get a crash because the object is already released.

To fix it, get rid of the useless first line, and also get rid of the incorrect release.


Without knowing the memory management of all your properties, it’s hard to see exactly what’s going on. But take a look here:

    for (int i = 0; i < count; i++) {
        retrievedAnalysisData = [[RetrievedAnalysisData alloc] init];
        retrievedAnalysisData = [sortedRetrievedAnalysisDataList objectAtIndex:i];

        [retrievedAnalysisIDarray addObject:retrievedAnalysisData.ID];
        [retrievedAnalysisData release];
    }

You call -release on an autoreleased object (retrievedAnalysisData). Try this instead:

    for (int i = 0; i < count; i++) {
        retrievedAnalysisData = [sortedRetrievedAnalysisDataList objectAtIndex:i];

        [retrievedAnalysisIDarray addObject:retrievedAnalysisData.ID];
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜