【问题标题】:Memory Leak from NSMutableArray and NSDictionaryNSMutableArray 和 NSDictionary 的内存泄漏
【发布时间】:2011-05-20 11:54:12
【问题描述】:

我有内存泄漏问题,也许有人可以帮助我。 我尝试为 pList 加载 NSMutable 数组并在 TablevView 中显示一些元素

在我声明的 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

这会导致内存泄漏

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];

这里是dealloc

- (void)dealloc {

NSLog(@"Bestellung dealloziert");

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

[bestellListe                   release];
[myTable                        release];

[super dealloc];

谁能帮帮我,我真的是新手。

【问题讨论】:

  • 你能把所有代码编辑并放入格式化的代码块中吗,bitte。

标签: objective-c cocoa memory-leaks nsdictionary


【解决方案1】:

您的属性合成方法会自动保留接收到的对象:

@property (nonatomic, retain) NSMutableArray *bestellListe;

所以当你这样做时:

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

您的 bestellListe 的保留计数为 2(alloc + 该属性的保留)。

在你的 dealloc 上你只释放一次:

[bestellListe release];

更简单的解决方案似乎在创建它时只保留一次,使用自动释放的初始化程序,例如:

self.bestellListe = [NSMutableArray arrayWithContentsOfFile:filePath];

希望对你有帮助

【讨论】:

    猜你喜欢
    • 2011-11-08
    • 2011-05-27
    • 1970-01-01
    • 1970-01-01
    • 2011-09-11
    • 2012-07-29
    • 2011-01-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多