因此當我嘗試運行下面的代碼時,我最終得到了一個EXC_BAD_ACCESS錯誤。它發生在我嘗試釋放一個NSMutableArray retrieveAnalysisDataList。該數組是retrieveAnalysisData對象的列表。如果我嘗試釋放數據列表,或者如果我使用autorelease設置init,我會得到相同的結果。我有點猜測它與代碼的排序部分有關,因爲我沒有retrieveAnalysisIDarray這個問題。釋放一個NSMutableArray導致EXC_BAD_ACCESS錯誤
有什麼想法?
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 objectForKey: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;
}
好的,改變了代碼,它的工作。進行分析併發布數據列表,泄漏消失了!非常感謝你,這是我在代碼中的幾個地方遇到的問題,所以我非常感激。我對obj-c和學習還很陌生,剛開始使用自定義對象。 你能否進一步解釋原代碼中會發生什麼?我創建了一個新的RetrievedAnalysisData對象,但是當我嘗試從我的sortedRetrievedAnalysisDataList中爲它分配一個RetrievedAnalaysisData對象時,我該如何將它扔掉? – Log139