开发者

Memory management with pushViewController

开发者 https://www.devze.com 2023-03-05 06:37 出处:网络
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:

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 retainCo开发者_StackOverflow中文版unt]);

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!


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


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.


When to use -retainCount?

retaincount seems to be useless


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!


The retainCount property depends on how the UINavigationController deals with the controller you are pushing. If it is retained various times inside the UIViewController object you should not be worried. The important thing is that when you use that object, you release it when you do not need it any longer.

E.g. in this case, since you are pushing a new controller to the navigator, you should do this:

DofView* dofView = [[DofView alloc] initWithNibName:@"DofView" bundle:nil];
[self.navigationController pushViewController:dofView animated:YES];
[dofView release];

The retain count for the object is probably not updated instantaneously, so you should not really care about that count.

If you think that you are leaking memory, you should use the profiling tools inside Xcode (search for memory leaks).

Hope this helps.


do not rely on retain count. in past i also try to check retain count for memory management but it always confuse me(check this link when retain count confuse me[http://stackoverflow.com/q/5483357/644149] . just follow simple guide line then u will have not memory leaks in ur apps

when u use alloc, new, copy, retain then you should also do release that object. simple


The release call doesn't take effect immediately, and so the retain count may not look correct. You could use the Leaks tool to track memory issues, but so long as you follow the memory mgmt rules, you should be okay, generally.

0

精彩评论

暂无评论...
验证码 换一张
取 消