NSMutableArray 和 NSDictionary 的内存泄漏

发布于 2024-10-06 10:17:20 字数 3962 浏览 0 评论 0原文

我有内存泄漏问题,也许有人可以帮助我。 我尝试加载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中我在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];

然后我生成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

这会导致内存泄漏at

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

Here ist dealloc

- (void)dealloc {

NSLog(@"Bestellung dealloziert");

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

[bestellListe                   release];
[myTable                        release];

[super dealloc];

有人可以给我一些帮助吗,我对此真的很陌生。

I have a memomry leak problem, maybe somebody can help me.
I try to load an NSMutable array for a pList and show some elements in a TablevView

In the h.File I declare

@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

In the M.File viewDidLoad I load the pList in 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];

Then I generate the 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]; 

Afterwards I fill them from 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

This causes memoryleaks at

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

and

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

Here ist dealloc

- (void)dealloc {

NSLog(@"Bestellung dealloziert");

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

[bestellListe                   release];
[myTable                        release];

[super dealloc];

Can somebody give me some help, I'm really new in that.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

孤单情人 2024-10-13 10:17:20

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

@property (nonatomic, retain) NSMutableArray *bestellListe;

因此,当您这样做时:

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

您的 bestellListe 的保留计数为 2(分配 + 属性的保留)。

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

[bestellListe release];

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

self.bestellListe = [NSMutableArray arrayWithContentsOfFile:filePath];

希望这可以帮助

your property synthesized method automatically retains received objects :

@property (nonatomic, retain) NSMutableArray *bestellListe;

So when you do :

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

Your bestellListe has a retain count of 2 (the alloc + the retain from the property).

On your dealloc you only release it once :

[bestellListe release];

The simpler solution seems to retain it only once when creating it, with an autoreleased initializer like :

self.bestellListe = [NSMutableArray arrayWithContentsOfFile:filePath];

Hope this can help

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文