NemeSys
NemeSys

Reputation: 555

Large retain count with a recent created object. Objective-C

I'm getting an strange case of excessive retain counts for a view controller that I'm loading when a button is pushed.

This is the code:

-(IBAction)new
{
    if (!viewSpace)
        viewSpace = [[ViewSpace alloc] initWithNibName:@"ViewSpace" bundle:nil];

    viewSpace.delegate = self;

    viewSpace.view.frame = CGRectMake(0, 0, viewSpace.view.frame.size.width, viewSpace.view.frame.size.height);

    [self presentModalViewController:viewSpace animated:YES];

    NSLog(@"Count Retain: %d",[viewSpace retainCount]);

}
-(void)viewSpaceWasDissmissed:(id)sender
{
    [self dismissModalViewControllerAnimated:YES];
    [viewSpace release];    
    NSLog(@"Count Retain: %d",[viewSpace retainCount]);
}

When the IBAction New is executed first time, the retain count is 5 when just is created. (It must be 1).

When the ViewSpace object must be unload calls viewSpaceWasDismissed function in order to remove the modal view and release the previous object.

The problem is that never the retain count reach 0 and the dealloc method of ViewSpace never is called causing memory leaks.

My question is how is possible that a recently created ViewController have 5 retains? I made sure that is never created before.

Thanks.

Upvotes: 0

Views: 262

Answers (4)

Abizern
Abizern

Reputation: 150565

You're doing two things wrong here:

  1. The Current view controller retains the modally presented view controller and releaseds it when it is dismissed. So you should release viewSpace after it is presented, and you don't need the release message in the dismissModalViewController method. As an aside ViewSpace is a poor name for a view controller. I had to read to the line where you are presenting it as a view controller before I knew it was a view controller. I think ViewSpaceController is a more descriptive name.
  2. You are using retainCount which is always a bad idea. All that matters is that in your new method you created an owned object (with the alloc) and you balanced that ownership with a release (or at least you will do when you put in the correction I suggested in point 1) That's it. You took ownership of an object and you released it. The retainCount method tells you absolutely nothing that can be of any use to you. Don't do it. Just balance ownerships with release, and that is all that matters.

Upvotes: 3

lukecampbell
lukecampbell

Reputation: 15256

I'm not 100% sure of every count but here are some:

  • Instantiation - 1
  • NIB - 1+
  • Strong Properties (1+)

Additionally any properties that list it as a strong property (in ARC). I noticed that when you launch a nib and you use components of the controller in the nib design, it will increase reference counts (in a strong manner) on the controller instance.

Upvotes: -3

Daniel
Daniel

Reputation: 1087

Check the documentation for -retainCount. I believe it says that you should not be calling it yourself - you just need to take care of any retains that you cause, and don't worry about the 'actual' retain count.

Upvotes: 3

Catfish_Man
Catfish_Man

Reputation: 41801

Cocoa is probably retaining the view controller 4 times internally for reasons of its own. This isn't a problem.

More generally, the -retainCount method is useless for reasons like this, and you should never call it. It will not help you, and it will confuse you.

To debug your leak, I suggest using the leaks Instrument, inspecting the object, and analyzing where each retain and release is coming from to determine whether any are incorrect.

Upvotes: 4

Related Questions