NemeSys
NemeSys

Reputation: 555

Memory leak in a class with 3 NSMutableArrays. Why?

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.

Upvotes: 0

Views: 168

Answers (5)

Padavan
Padavan

Reputation: 1074

I think, that leak are in arrays filling code, not init. For example, you alloc and init some NSString and put it in your NSMutable array without release or autorelease it. Or something like this. Make shure that you filling your arrays right way.

Upvotes: 0

I think that you are getting the leak because you call your initialize message more than once and you are not releasing the variables.

Normally, you should use setters or getters to access your ivars, so the appropiate operations are called (like the release message before assigning a new value on setter).

And remember to call [super dealloc] as the last instruction for your dealloc ;)

Upvotes: 1

DAS
DAS

Reputation: 1941

Just init them when you need it. Calling alloc and init holds to arrays whose content is nil. Call alloc initWithObjects: and fill the arrays.

Upvotes: -1

CStreel
CStreel

Reputation: 2642

Without seeing the code where you are initialising the RDItem i can't say for sure.

But i expect that you are not releasing your previous RDItem before creating your new one. So post the code where you are creating your RDItems so we can say for sure

Upvotes: 0

Saurabh Passolia
Saurabh Passolia

Reputation: 8109

This is a better suggested implementation

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

Upvotes: 2

Related Questions