Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Memory management with pushViewController

I'm still not that great with memory management techniques, and wondered if someone could explain this weird behaviour to me. Consider these 3 pieces of code which I have been testing:

DofView* dofView = [[DofView alloc] initWithNibName:@"DofView" bundle:nil];
NSLog(@"dof retain count = %d", [dofView retainCount]); 

This logs: retain count = 1. This is fine.

DofView* dofView = [[DofView alloc] initWithNibName:@"DofView" bundle:nil];
[dofView release];
NSLog(@"dof retain count = %d", [dofView retainCount]); 

This logs: retain count = 1. Shouldn't it be 0??

DofView* dofView = [[DofView alloc] initWithNibName:@"DofView" bundle:nil];
[self.navigationController pushViewController:dofView animated:YES];
NSLog(@"dof retian count = %d", [dofView retainCount]);

This logs: retain count = 5. I have NO idea why its five?

Cany anyone shed any light on this at all? Im concerned that I'm eating up memory every time I'm creating a new view.

Thanks!

like image 560
rich Avatar asked May 10 '11 11:05

rich


4 Answers

Do not rely on retainCount for memory analysis. Go through the reference documents on Memory Management for further info

like image 158
visakh7 Avatar answered Nov 06 '22 16:11

visakh7


Looking at retain count is strongly discouraged, it won't give you any valuable information. If you want to know if something is getting properly released you should put a breakpoint or a log-entry in the dealloc method of your class - when dealloc is called the object is very soon to be released. Apart from the instruments app, this is what I use to find retain-cycles.

like image 34
iceydee Avatar answered Nov 06 '22 17:11

iceydee


When to use -retainCount?

retaincount seems to be useless

like image 26
Zaky German Avatar answered Nov 06 '22 15:11

Zaky German


It's important to know release happens immediately (autorelease takes a while).

So why does your object still have a retain count of 1?

Because it's been deallocated - it's not your object anymore, it's just some free memory!

Try doing this:

NSObject* o = [[NSObject alloc] init];
NSLog(@"retain count = %d", [o retainCount]); 
[o retain];
NSLog(@"retain count = %d", [o retainCount]); 
[o release];
NSLog(@"retain count = %d", [o retainCount]); 
[o release];
NSLog(@"retain count = %d", [o retainCount]); 

You get the output

retain count = 1
retain count = 2
retain count = 1
retain count = 1

You might expect the last line to say 0, not 1. However, it won't bother decrementing the retain count if it's going to be released - what would be the point!

Actually, the fact that the last line outputs anything at all without crashing is lucky - because the object has been deallocated, there's nothing stopping that memory being used by something else - it's effectively just random data now. This is exactly the kind of bug that causes EXC_BAD_ACCESS crashes :)

In fact, you get a compiler analyser warning on the final NSLog because you're passing a message to an object that doesn't exist any more :)


As for the retain count of 5 - I can only echo the other answers - you don't know what's going on inside objects - just because you only called retain once doesn't mean that no-one else called retain :)

As long as you release for every retain you make, you're doing the right thing.

If you're worried about leaks, use the profiler and check!

like image 39
deanWombourne Avatar answered Nov 06 '22 17:11

deanWombourne