I’m getting a memory leak with instruments in a class that I’ve created. This is the class:
.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
This class have 3 NSMutableArray where I’ll store data. In order to prepare and initialice this class, I’ve developed the method initialize where the 3 arrays are created. In dealloc are released.
The leaks tool detects a leak each time this class is used because the initialize method.
Which is the best way to initialize these arrays?
Thanks.
EDIT
Hi I’ve solved the leak with RDItem but now appears another lear in a very similar class:
.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];
}
Now I’m getting a leak in these lines:
self.items = [NSMutableArray array];
self.colors = [NSMutableArray array];
self.textures = [NSMutableArray array];
self.styles = [NSMutableArray array];
Anyone can explain why is happening in this class and not in RDItem class now? Are the same 🙁
Thanks.
This is a better suggested implementation