【问题标题】:Memory leak in a class with 3 NSMutableArrays. Why?具有 3 个 NSMutableArrays 的类中的内存泄漏。为什么?
【发布时间】:2012-02-20 09:06:16
【问题描述】:

我创建的类中的仪器出现内存泄漏。这是课程:

.h

#import <Foundation/Foundation.h>


@interface RDItem : NSObject {

}
@property int Id;
@property (nonatomic, retain) NSString *nombre;
@property (nonatomic, retain) NSString *thumbnail;
@property (nonatomic, retain) NSString *thumbnailPush;
@property int defaultColorId;
@property int idTema;

@property (nonatomic, retain) NSString *selectedFrame;
@property (nonatomic, retain) NSString *mergedFrame;

@property (nonatomic, retain) NSMutableArray *colors;
@property (nonatomic, retain) NSMutableArray *textures;
@property (nonatomic, retain) NSMutableArray *styles;

-(void)initialize;
@end

.m

#import "RDItem.h"


@implementation RDItem
@synthesize Id;
@synthesize nombre;
@synthesize thumbnail;
@synthesize thumbnailPush;
@synthesize defaultColorId;
@synthesize idTema;
@synthesize selectedFrame;
@synthesize mergedFrame;

@synthesize colors;
@synthesize textures;
@synthesize styles;

-(void)initialize
{
    colors = [[NSMutableArray alloc] init];
    textures = [[NSMutableArray alloc] init];
    styles = [[NSMutableArray alloc] init];
}
-(void)dealloc
{
    [colors release];
    [textures release];
    [styles release];
}
@end

这个类有 3 个 NSMutableArray,我将在其中存储数据。为了准备和初始化这个类,我开发了方法 initialize 来创建 3 个数组。在 dealloc 中被释放。

每次使用此类时,泄漏工具都会检测到泄漏,因为初始化方法。

初始化这些数组的最佳方法是什么?

谢谢。

编辑

您好,我已经用 RDItem 解决了泄漏问题,但现在又出现了一个非常相似的类:

.h

#import <Foundation/Foundation.h>

@interface RDTema : NSObject {

}
@property int Id;
@property (nonatomic, retain) NSString *idManifest;
@property (nonatomic, retain) NSString *idTema;
@property (nonatomic, retain) NSString *nombre;
@property (nonatomic, retain) NSString *thumbnail;
@property (nonatomic, retain) NSString *thumbnailPush;

@property (nonatomic, retain) NSMutableArray *items;

@property (nonatomic, retain) NSMutableArray *colors;
@property (nonatomic, retain) NSMutableArray *textures;
@property (nonatomic, retain) NSMutableArray *styles;

-(void)initialize;
@end

.m

#import "RDTema.h"


@implementation RDTema
@synthesize Id;
@synthesize idManifest;
@synthesize idTema;
@synthesize nombre;
@synthesize thumbnail;
@synthesize thumbnailPush;

@synthesize items;

@synthesize colors;
@synthesize textures;
@synthesize styles;

-(void)initialize
{
    /*
    self.items = [[NSMutableArray alloc] init];
    self.colors = [[NSMutableArray alloc] init];
    self.textures = [[NSMutableArray alloc] init];
    self.styles = [[NSMutableArray alloc] init];
     */

    self.items = [NSMutableArray array];
    self.colors = [NSMutableArray array];
    self.textures = [NSMutableArray array];
    self.styles = [NSMutableArray array];
}
-(void)dealloc
{
    [idManifest release];
    [idTema release];
    [nombre release];
    [thumbnail release];
    [thumbnailPush release];

    [items release];
    [colors release];
    [textures release];
    [styles release];

    [super dealloc];
}

现在我在这些行中发现了一个漏洞:

self.items = [NSMutableArray array];
self.colors = [NSMutableArray array];
self.textures = [NSMutableArray array];
self.styles = [NSMutableArray array];

任何人都可以解释为什么现在发生在这个类而不是 RDItem 类中?都是一样的:(

谢谢。

【问题讨论】:

  • 虽然实现似乎并没有使用您共享的代码行造成真正的内存泄漏。虽然我分享了一个更好的实现。
  • 您的代码没有泄漏。但我有几个问题:1)你调用了多少次“初始化”方法? 2)你释放插入可变数组的对象吗?

标签: iphone xcode instruments memory-leaks


【解决方案1】:

这是一个更好的建议实现

-(void)initialize
{
    self.colors = [NSMutableArray array];
    self.textures = [NSMutableArray array];
    self.styles = [NSMutableArray array];
}
-(void)dealloc
{
    self.colors = nil;
    self.textures = nil;
    self.styles = nil;
}

【讨论】:

  • 别忘了[super dealloc];
  • 这是应用的解决方案 :) 谢谢!
  • @samfisher,你能解释一下为什么你选择将它们设置为nil而不是releaseing它们吗?,我认为nil只为viewDidUnload中的IBOutlets设置。编辑: 对不起我的错误,我以为你是allocing 他们
  • @iNoob, self.object = nil 调用你的setter,它将释放旧值,将成员设置为nil
  • 请参阅此处 (stackoverflow.com/questions/8576593/…),了解使用 @property@synthesize 时“幕后”发生的事情的描述。可能会帮助您更多地理解这些概念
【解决方案2】:

我认为你得到了泄漏,因为你不止一次调用你的初始化消息并且你没有释放变量。

通常,您应该使用 setter 或 getter 来访问您的 ivars,因此会调用适当的操作(例如在 setter 上分配新值之前的发布消息)。

记得调用 [super dealloc] 作为你的 dealloc 的最后一条指令;)

【讨论】:

    【解决方案3】:

    没有看到初始化 RDItem 的代码,我无法确定。

    但我希望您在创建新的 RDItem 之前不会发布以前的 RDItem。因此,将代码发布到您创建 RDItems 的位置,以便我们可以肯定地说

    【讨论】:

      【解决方案4】:

      我认为,泄漏是在填充代码的数组中,而不是 init。例如,您分配并初始化一些 NSString 并将其放入您的 NSMutable 数组中,而无需释放或自动释放它。或者类似的东西。确保您以正确的方式填充数组。

      【讨论】:

        【解决方案5】:

        您需要时只需init 他们。调用allocinit 保存内容为nil 的数组。调用allocinitWithObjects: 并填充数组。

        【讨论】:

          猜你喜欢
          • 2011-03-05
          • 2012-05-02
          • 1970-01-01
          • 1970-01-01
          • 2011-02-01
          • 1970-01-01
          • 2011-10-25
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多