具有3个NSMutableArrays的类中的内存泄漏。为什么?

时间:2012-01-27 10:54:05

标签: iphone xcode instruments memory-leaks

我正在使用我创建的类中的乐器进行内存泄漏。这是班级:

·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,我将存储数据。为了准备和初始化这个类,我开发了初始化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课程中?是相同的:(

感谢。

5 个答案:

答案 0 :(得分:2)

这是一个更好的建议实施

-(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;
}

答案 1 :(得分:1)

我认为您收到泄漏是因为您多次调用初始化消息并且您没有发布变量。

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

并记得将[super dealloc]称为你的dealloc的最后一条指令;)

答案 2 :(得分:0)

如果没有看到初始化RDItem的代码,我无法肯定地说。

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

答案 3 :(得分:0)

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

答案 4 :(得分:-1)

在您需要时只需init。调用allocinit会保留内容为nil的数组。致电alloc initWithObjects:并填写数组。