我在创建的类中发现了仪器的内存泄漏。这是类:使用3个NSMutableArrays的类中的内存泄漏。为什么?
.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类现在是怎么回事?是相同的:(
感谢。
尽管这个实现似乎并没有为你共享的代码行产生真正的内存泄漏。尽管我已经分享了更好的实施。 – samfisher 2012-01-27 11:01:00
你的代码没有泄漏。但我有几个问题:1)你称之为“初始化”方法多少次? 2)你释放插入到可变数组中的对象吗? – SVGreg 2012-01-27 11:07:33