2010-12-05 118 views
0

我有一個memomry泄漏問題,也許有人可以幫助我。 我嘗試加載用於一個的plist陣列NSMutable並顯示一些元件以TablevView內存泄漏NSMutableArray和NSDictionary

在h.File我聲明

@interface Bestellung : UIViewController <UITableViewDelegate, UITableViewDelegate> { 

NSMutableArray *bestellteVorspeisen; 
NSMutableArray *bestellteVorspeisenPreis; 
NSMutableArray *bestellteVorspeisenDetails; 
NSMutableArray *bestellteVorspeisenBild; 
NSMutableArray *bestellteVorspeisenNummer; 

NSMutableArray *bestellListe; 


IBOutlet UITableView *myTable; 


} 
@property (nonatomic, retain) NSMutableArray *bestellListe; 

@property (nonatomic,retain) NSMutableArray *bestellteVorspeisen; 
@property (nonatomic,retain) NSMutableArray *bestellteVorspeisenPreis; 
@property (nonatomic,retain) NSMutableArray *bestellteVorspeisenDetails; 
@property (nonatomic,retain) NSMutableArray *bestellteVorspeisenBild; 
@property (nonatomic,retain) NSMutableArray *bestellteVorspeisenNummer; 

@end 

在M.File viewDidLoad中我加載的plist在bestellListe

NSFileManager *fileManager = [NSFileManager defaultManager]; 
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [paths objectAtIndex:0]; 
    NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"Bestellung.plist"]; 
    success = [fileManager fileExistsAtPath:filePath]; 
    if (!success) 
{  
    NSString *path = [[NSBundle mainBundle] pathForResource:@"Bestellung" ofType:@"plist"]; 
     success = [fileManager copyItemAtPath:path toPath:filePath error:&error]; 
    } 
    self.bestellListe = [[NSMutableArray alloc] initWithContentsOfFile:filePath]; 

然後我產生MutableArray

bestellteVorspeisen  = [[NSMutableArray alloc] initWithCapacity:1]; 
bestellteVorspeisenPreis  = [[NSMutableArray alloc] initWithCapacity:1]; 
bestellteVorspeisenDetails = [[NSMutableArray alloc] initWithCapacity:1]; 
bestellteVorspeisenBild  = [[NSMutableArray alloc] initWithCapacity:1]; 
bestellteVorspeisenNummer = [[NSMutableArray alloc] initWithCapacity:1]; 

後來我填補他們從bestellListe

for (NSDictionary *bestellDict in bestellListe) 
    { if ([[bestellDict objectForKey:@"Tisch"] isEqualToString: 
[NSString stringWithFormat:@"%i",TischNummer]]) 
     {if ([[bestellDict objectForKey: kBestellungKategorieString] isEqualToString: @"Vorspeise"]) 
      [bestellteVorspeisen   addObject: [bestellDict objectForKey:kBestellungNameString]]; 
       [bestellteVorspeisenPreis  addObject: [bestellDict objectForKey:kBestellungPreisString]]; 
       [bestellteVorspeisenDetails  addObject: [bestellDict objectForKey:kBestellungDetailString]]; 
       [bestellteVorspeisenBild  addObject: [bestellDict objectForKey:kBestellungBildString]]; 
       [bestellteVorspeisenNummer  addObject: [bestellDict objectForKey:kBestellungNummer]]; 
} // if 
} // if 
} // for 

這導致memoryleaks在

self.bestellListe = [[NSMutableArray alloc] initWithContentsOfFile:filePath]; 

bestellteVorspeisen  = [[NSMutableArray alloc] initWithCapacity:1]; 
bestellteVorspeisenPreis  = [[NSMutableArray alloc] initWithCapacity:1]; 
bestellteVorspeisenDetails = [[NSMutableArray alloc] initWithCapacity:1]; 
bestellteVorspeisenBild  = [[NSMutableArray alloc] initWithCapacity:1]; 
bestellteVorspeisenNummer = [[NSMutableArray alloc] initWithCapacity:1]; 

這裏IST的dealloc

- (void)dealloc { 

NSLog(@"Bestellung dealloziert"); 

[bestellteVorspeisen   release]; 
[bestellteVorspeisenPreis  release]; 
[bestellteVorspeisenDetails  release]; 
[bestellteVorspeisenBild  release]; 
[bestellteVorspeisenNummer  release]; 

[bestellListe     release]; 
[myTable      release]; 

[super dealloc]; 

有人能給我一些幫助,我那真的很新鮮。

+0

你可以編輯並把所有的代碼放在格式化的代碼塊,bitte。 – 2010-12-05 11:38:54

回答

5

你的財產合成方法自動保存接收到的對象:

@property (nonatomic, retain) NSMutableArray *bestellListe; 

所以,當你這樣做:

self.bestellListe = [[NSMutableArray alloc] initWithContentsOfFile:filePath]; 

你bestellListe具有2的保留計數(在ALLOC +從屬性的保留) 。

在你的dealloc只釋放一次:

[bestellListe release]; 

更簡單的解決方案似乎在創建時,它保留它只有一次,有像自動釋放初始化:

self.bestellListe = [NSMutableArray arrayWithContentsOfFile:filePath]; 

希望這可以幫助

+0

非常感謝,解決了這個問題。 – user3449120 2011-02-17 18:58:28