2011-07-26 47 views
0

我認爲存在內存管理問題。當它嘗試分配第二次(運行代碼並進行第一次分配但第二次分配使其死亡)時,我收到「EXC_BAD_ACCESS」錯誤。內存管理問題?收到「EXC_BAD_ACCESS」錯誤

我有一個函數,它將採取自定義對象(包含日期和名稱變量)的數組。我需要將這個數組排序爲特定於月份的數組(所有的節點都將轉到aprilList,可能轉換爲mayList等)。

.h文件中

@class SceneData; 

@interface DetailedSceneViewController : UIViewController <UITableViewDataSource, UITableViewDelegate> 
{ 
    UITableView *mainTableView; 
    SceneData *sceneData; 
    NSArray *monthsArray; 
} 


@end 

.m文件

#import "SceneData.h" 


- (void) sortMonths { 
Agri_ImaGIS_iPhoneAppDelegate *dataCenter = (Agri_ImaGIS_iPhoneAppDelegate *) [[UIApplication sharedApplication] delegate]; 

NSMutableArray *janList, *febList, *marList, *aprList, *mayList, *junList, *julList, *augList, *sepList, *octList, *novList, *decList = [[NSMutableArray alloc] init]; 

int count = [dataCenter.sortedDataList count]; 

NSLog(@"count: %d", count); 

for (int i = 0; i < count; i++) 
{ 
    sceneData = [dataCenter.sortedDataList objectAtIndex:i]; 
    if ([sceneData.name rangeOfString:@"Jan" ].location != NSNotFound) 
    { 
     NSLog(@"jan name: %@", sceneData.name); 
     [janList addObject: sceneData]; 
    } 
    else if ([sceneData.name rangeOfString:@"Feb" ].location != NSNotFound) 
    { 
     NSLog(@"feb name: %@", sceneData.name); 
     [febList addObject:sceneData]; 
    } 
    else if ([sceneData.name rangeOfString:@"Mar" ].location != NSNotFound) 
    { 
     NSLog(@"Mar name: %@", sceneData.name); 
     [marList addObject:sceneData]; 
    } 
    else if ([sceneData.name rangeOfString:@"Apr" ].location != NSNotFound) 
    { 
     NSLog(@"Apr name: %@", sceneData.name); 
     [aprList addObject:sceneData]; 
    } 
    else if ([sceneData.name rangeOfString:@"May" ].location != NSNotFound) 
    { 
     NSLog(@"May name: %@", sceneData.name); 
     [mayList addObject:sceneData]; 
    } 
    else if ([sceneData.name rangeOfString:@"Jun" ].location != NSNotFound) 
    { 
     NSLog(@"Jun name: %@", sceneData.name); 
     [junList addObject:sceneData]; 
    } 
    else if ([sceneData.name rangeOfString:@"Jul" ].location != NSNotFound) 
    { 
     NSLog(@"Jul name: %@", sceneData.name); 
     [julList addObject:sceneData]; 
    } 
    else if ([sceneData.name rangeOfString:@"Aug" ].location != NSNotFound) 
    { 
     NSLog(@"Aug name: %@", sceneData.name); 
     [augList addObject:sceneData]; 
    } 
    else if ([sceneData.name rangeOfString:@"Sep" ].location != NSNotFound) 
    { 
     NSLog(@"Sep name: %@", sceneData.name); 
     [sepList addObject:sceneData]; 
    } 
    else if ([sceneData.name rangeOfString:@"Oct" ].location != NSNotFound) 
    { 
     NSLog(@"Oct name: %@", sceneData.name); 
     [octList addObject:sceneData]; 
    } 
    else if ([sceneData.name rangeOfString:@"Nov" ].location != NSNotFound) 
    { 
     NSLog(@"Nov name: %@", sceneData.name); 
     [novList addObject:sceneData]; 
    } 
    else if ([sceneData.name rangeOfString:@"Dec" ].location != NSNotFound) 
    { 
     NSLog(@"Dec name: %@", sceneData.name); 
     [decList addObject:sceneData]; 
    } 
} 
} 

我在處理sceneData對象不正確?

回答

2

您沒有正確初始化數組的:

NSMutableArray *janList, *febList, *marList, *aprList, *mayList, *junList, *julList, *augList, *sepList, *octList, *novList, *decList = [[NSMutableArray alloc] init]; 

此語法僅初始化最後一個指針......你應該使用:

NSMutableArray *janList = [[NSMutableArray alloc] init]; 
NSMutableArray *febList = [[NSMutableArray alloc] init]; 
... 
NSMutableArray *decList = [[NSMutableArray alloc] init]; 

在另一方面,所有的對象都泄漏,因爲(如果我沒有失去你的代碼中的東西),他們是本地變量(不autoreleased),你不會呼籲釋放他們......

+0

哦,哈哈!多數民衆贊成我得到懶惰。 xcode沒有給我任何錯誤或警告,所以我認爲所有的人都喜歡這樣的工作。你是對的,他們都漏了,我還沒有足夠的時間去擔心這個問題。謝謝您的幫助! – Log139

+0

@ Log139從開始實施內存管理是一個好主意。稍後添加代碼是屁股中容易出錯的麻煩。設計,安全和手動內存管理最終不是你要做的。 – PeyloW