user3092283
user3092283

Reputation: 1

Objective-C Memory Leak on addSubview

The leak instruments warn me about a memory leak related to this part of code:

[self.contview addSubview:nav.view];

Here are how I manage the view:

    [nav.view removeFromSuperview];
    self.nav = [[[destinationClass alloc] initWithNibName:pagename bundle:nil] autorelease];
   [self.contview addSubview:nav.view];

Is it normal that the self.nav has a retainCount of 2 just after been allocated?Could this be related to the memory leak?

I'm very new to the memory management can someone give me some help?

Many Thanks

Upvotes: 0

Views: 512

Answers (1)

Marko Hlebar
Marko Hlebar

Reputation: 1973

Assuming nav is a strong (retain) property, it retains the view controller you are assigning here:

self.nav = [[[destinationClass alloc] initWithNibName:pagename bundle:nil] autorelease];

effectively, the retain count after this line of code is 1; +2 for alloc and retain and -1 for autorelease. Generally you should never use retainCount method to determine the actual retain count of the object, maybe this answer will give you more insight why. Every alloc, retain or copy call should be matched with a release or autorelease call. You should add a matching release call in dealloc method of your class

-(void) dealloc {
    [_nav release];
    _nav = nil;
    [super dealloc];
}

Don't use manual memory management, use ARC, it will make your life much easier :)

Upvotes: 2

Related Questions