简体   繁体   中英

iPhone Memory Management

Hello Stackoverflow fellow family members!

I've got question regarding memory management in iPhone.

What I did understand was below method

-(void) dealloc
{
    // something else to release whatever
    // such as Object Created using keyword 'alloc'
    // but also Object destroy here its retain value reaches iff 0
    // if I do put here NSLog(@"%d", [obj retainCount]); and when it reaches
    // not equal to 0 means failure with memory leak.
    [super dealloc];
}

So am I understand right? or It is still alight even if retain count reachs > 0 here?

The reason I ask about this question because,

I checked with

NSLog(@"%d", obj.retainCount);

to check the retain count of the object and received value 3. So I tried to release here 3 times to make retainCount here equal to 0, but compiler gives me critical error.

Please, I'm new to the memory de-allocation and retain, release.

Object that I used was 'UIImageView' object and created another instance as,

UIImageView *imageView = //da da~ with UIImage
UIImageView *instance;
// at this point retain count was '1'
instance = imageView;
//[imageView retain];
// at this point retain count was '2'
[self.view addSubView: imageView];
// at this point retain count was '3'
[imageView release];// crashes
// at this point retain count was '2'

but if I do

// but if I add retain on the 'instance = imageView'
// such as
instance = imageView; // then
[imageView retain];
// works but still count is 2...

Thank You.

retainCount is not a reliable debugging-tool:

  • other objects might still hold references to obj
  • there are objects that you can't destroy (eg string constants )
  • objects are deallocated if released with a retain count of 1

What you should take care of instead:

  • balancing references you have with the right amount of release / autorelease
  • using the Analyzer
  • using Leaks

The normal process for overriding dealloc is to release any objects that have been previously retained (or alloc'ed) by this instance.

So if somewhere else in the object you have called an alloc or retain method your dealloc would look like:

-(void)someOtherMethod
{
    UIImageView *imageView = //da da~ with UIImage
    UIImageView *instance;
    instance = imageView;
    [instance retain];
}

-(void) dealloc
{
    //release any retained objects here
    [instance release]        
    [super dealloc];
}

Note that it doesn't matter if the release count hasn't dropped to zero after your particular release, that just means that some other bit of code has also retained the object memory (and that other bit of code will be responsible for releasing it).

Hope this helps.

That's not correct and you should rarely use retainCount. It does not have to be 0 at this point as other objects could have references to the objects you are releasing. What is important to do in dealloc is to release objects that you have ownership on. Which would be objects created with alloc or new etc.

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