0 cocoa memory-leaks objective-c nsdictionary
我有一个memomry泄漏问题,也许有人可以帮助我.我尝试为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
Run Code Online (Sandbox Code Playgroud)
在M.File viewDidLoad中,我在bestellListe中加载了pList
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];
Run Code Online (Sandbox Code Playgroud)
然后我生成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];
Run Code Online (Sandbox Code Playgroud)
然后我从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
Run Code Online (Sandbox Code Playgroud)
这导致记忆泄漏
self.bestellListe = [[NSMutableArray alloc] initWithContentsOfFile:filePath];
Run Code Online (Sandbox Code Playgroud)
和
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];
Run Code Online (Sandbox Code Playgroud)
这是dealloc
- (void)dealloc {
NSLog(@"Bestellung dealloziert");
[bestellteVorspeisen release];
[bestellteVorspeisenPreis release];
[bestellteVorspeisenDetails release];
[bestellteVorspeisenBild release];
[bestellteVorspeisenNummer release];
[bestellListe release];
[myTable release];
[super dealloc];
Run Code Online (Sandbox Code Playgroud)
有人可以给我一些帮助,我真的很新.
您的属性合成方法自动保留收到的对象:
@property (nonatomic, retain) NSMutableArray *bestellListe;
Run Code Online (Sandbox Code Playgroud)
所以当你这样做时:
self.bestellListe = [[NSMutableArray alloc] initWithContentsOfFile:filePath];
Run Code Online (Sandbox Code Playgroud)
您的bestellListe的保留计数为2(alloc +属性的保留).
在你的dealloc上你只发布一次:
[bestellListe release];
Run Code Online (Sandbox Code Playgroud)
更简单的解决方案似乎只在创建它时保留一次,使用自动释放的初始化程序,如:
self.bestellListe = [NSMutableArray arrayWithContentsOfFile:filePath];
Run Code Online (Sandbox Code Playgroud)
希望这可以提供帮助