2011-09-02 35 views
0

在我的應用程序的某個時刻,我需要從文件加載的清單,讓我實現這個方法來加載列表:幫助內存泄漏:初始化的NSMutableArray從文件

-(void)loadList 
{ 
    NSString *filePath = [self dataFilePath]; //This is a method return the path of file 
    if([[NSFileManager defaultManager] fileExistsAtPath:filePath]) 
    { 
     NSMutableArray *tempArray = [[NSMutableArray alloc]initWithContentsOfFile:filePath]; 
     self.list = [[NSMutableArray alloc]initWithArray:tempArray]; 
     [tempArray release]; 
    } 
} 

的self.list是(保留)財產。

我覺得泄漏是從我的初始selfl.list [alloc]。我用

self.list = [[[NSMutableArray alloc]initWithArray:tempArray]autorelease]; 

但由於EXC_BAD_ACCESS,應用程序崩潰。所以我很困惑這裏如何解決這個問題。

感謝您的任何建議。

+0

嘗試此代碼'self.list = [NSMutableArray arrayWithArray:tempArray];' – Bonny

回答

3

剛分配,

self.list = tempArray; 

由於tempArray已經是一個數組,你不必從它創建另一個陣列。您可以直接分配到self.list

1
There is no need to allocate another time for array .So just assign directly 

    -(void)loadList 
    { 
NSString *filePath = [self dataFilePath]; //This is a method return the path of file 
if([[NSFileManager defaultManager] fileExistsAtPath:filePath]) 
{ 
    NSMutableArray *tempArray = [[NSMutableArray alloc]initWithContentsOfFile:filePath]; 
    self.list = [tempArray copy]; 
    [tempArray release]; 
} 
} 
+0

我這樣做了,但儀器仍然告訴我仍然存在泄漏... – Jing

+0

您正在做構建和分析? –

0

是您的列表屬性分配還是保留? 如果保留,那麼你應該改變這樣的:

self.list = [[NSMutableArray alloc]initWithArray:tempArray]; 

這樣:

self.list = [[NSMutableArray arrayWithArray:tempArray]; 
0

不要自動釋放它。 (我猜)。