Antonio MG
Antonio MG

Reputation: 20410

Memory leak after switching to @property declaration?

Why Im having a memory leak here:

.h:

@property (nonatomic, retain) NSDictionary *info;

and then in my .m:

@synthesize info = _info;

and Im having one leak here:

- (id)initWithData:(NSDictionary *)data
{
    self = [super init];
    if (self) {
        self.info = [[NSDictionary alloc] initWithDictionary:data];
    }
    return self;
}

And another leak here:

-(void)dealloc {
    [self.info release];
    [super dealloc];
}

Upvotes: 1

Views: 106

Answers (2)

Daniel
Daniel

Reputation: 22405

Your leak stems from your property declaration and the way you assign it, when you declare a property as "retain" using the setter will retain the var for you, so doing

self.info = [[NSDictionary alloc] initWithDictionary:data];

Retains the var, and since alloc also retain the var you get a +2 retain count, you should either autorelease the object or assign it like

 _info = [[NSDictionary alloc] initWithDictionary:data];

also when you release a property do so by setting it to nil

self.info=nil

Upvotes: 3

Carl D'Halluin
Carl D'Halluin

Reputation: 1072

Your constructor should use

self.info = [NSDictionary dictionaryWithDictionary:data];

Your dealloc should be simplified to

- (void)dealloc {
    self.info = nil;
    [super dealloc];
}

Upvotes: 1

Related Questions