初始化 NSMutableDictionary 时 iPhone 内存泄漏

发布于 2024-11-02 14:19:25 字数 783 浏览 1 评论 0原文

我发现添加到lossTerms 的每个对象(除了第一个对象)出现内存泄漏。该泄漏被描述为具有“A +1 保留计数(拥有引用)” - 我搜索了互联网并发现了几个线程提到在对象分配中使用“复制”。我尝试了这个,但我一定没有正确实现它,因为我仍然存在内存泄漏。

- (void)createTermData {

NSMutableArray *glossTerms=[[NSMutableArray alloc] init];

[glossTerms addObject:[[NSMutableDictionary alloc]
                       initWithObjectsAndKeys:@"Approximate",@"term",
                       @"Test",@"definition",@"Test2",@"example",nil]];

[glossTerms addObject:[[NSMutableDictionary alloc]
                       initWithObjectsAndKeys:@"Arithmetic Mean",@"term",
                       @"Test",@"definition",@"Test2",@"example",nil]];

termData=[[NSMutableArray alloc] initWithObjects:
            glossTerms,nil];

[glossTerms release];

如果你们中的任何人能够阐明这一点,我将非常感激。

谢谢

I am getting a memory leak for each object that is added to glossTerms (apart from the first object). The leak is described as having "A +1 retain count (owning reference)" - I have searched the Internet and have found a couple of threads which mention using "copy" inside the object allocation. I tried this but I must not have implemented it properly as I was still having memory leaks.

- (void)createTermData {

NSMutableArray *glossTerms=[[NSMutableArray alloc] init];

[glossTerms addObject:[[NSMutableDictionary alloc]
                       initWithObjectsAndKeys:@"Approximate",@"term",
                       @"Test",@"definition",@"Test2",@"example",nil]];

[glossTerms addObject:[[NSMutableDictionary alloc]
                       initWithObjectsAndKeys:@"Arithmetic Mean",@"term",
                       @"Test",@"definition",@"Test2",@"example",nil]];

termData=[[NSMutableArray alloc] initWithObjects:
            glossTerms,nil];

[glossTerms release];

If any of you are able to shed any light on this, I'd be very grateful.

Thank you

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

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

发布评论

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

评论(1

佞臣 2024-11-09 14:19:25

NSArray 将保留您添加到其中的任何对象并自动释放它,无论是在其生命周期结束时还是在您从数组中删除它时(如果它是可变的)。

当你分配一个对象时,它的保留计数是+1。

因此,当您分配一个对象并将其添加到数组时,它的保留计数为+2。这意味着您必须自动释放该对象(这就是我在您的情况下所做的,因为您是在添加的同一行上分配的),或者如果您有对指针的引用,则在添加它后释放它。就您而言,您没有参考,因此自动释放它是您最好的选择。

NSMutableArray *glossTerms=[[NSMutableArray alloc] init];

[glossTerms addObject:[[[NSMutableDictionary alloc]
                       initWithObjectsAndKeys:@"Approximate",@"term",
                       @"Test",@"definition",@"Test2",@"example",nil] autorelease]];

[glossTerms addObject:[[[NSMutableDictionary alloc]
                       initWithObjectsAndKeys:@"Arithmetic Mean",@"term",
                       @"Test",@"definition",@"Test2",@"example",nil] autorelease]];

termData=[[NSMutableArray alloc] initWithObjects:
            glossTerms,nil];

[glossTerms release];

FWIW,静态方法 [NSMutableDictionarydictionaryWith...] 自动返回一个自动释放的对象。

An NSArray will retain any object you add to it and release it automatically, either at the end of it's lifecycle or once you remove it from the array (if it's mutable).

When you allocate an object, it's retain count is +1.

Therefore, when you allocate an object and add it to an array, it's retain count is +2. This means that you must either autorelease the object (which is what I'd do in your case, since you're allocating on the same line you're adding) or release it after adding it if you have a reference to the pointer. In your case, you don't have a reference, so autoreleasing it is your best bet.

NSMutableArray *glossTerms=[[NSMutableArray alloc] init];

[glossTerms addObject:[[[NSMutableDictionary alloc]
                       initWithObjectsAndKeys:@"Approximate",@"term",
                       @"Test",@"definition",@"Test2",@"example",nil] autorelease]];

[glossTerms addObject:[[[NSMutableDictionary alloc]
                       initWithObjectsAndKeys:@"Arithmetic Mean",@"term",
                       @"Test",@"definition",@"Test2",@"example",nil] autorelease]];

termData=[[NSMutableArray alloc] initWithObjects:
            glossTerms,nil];

[glossTerms release];

FWIW, the static methods [NSMutableDictionary dictionaryWith...] return an autoreleased object automatically.

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