简体   繁体   中英

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

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 :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM