view based application - viewcontroller retainCount after [window addSubview:viewController.view];
i have an view based application and when i checked if the dealloc method was workin, i saw that doesnt...debug and debug, then using retainCount, i discover that my viewcontroller retaincount gets crazy on the program start...its easy to reproduce....
just start a new view based application template, and in the didFinishLaunchingWithOptions:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after app launch.
NSLog(@"i1 %i",[viewController retainCount]);
[window addSubview:viewController.view];
NSLog(@"i2 %i",[viewController retainCount]);
[window makeKeyAndVisible];
return YES;
}
i got on the console:
[Session started at 2010-08-07 09:57:34 -0300.]
2010-08-07 09:57:35.132 teste1[20367:207] i1 2
2010-08-07 09:57:35.138 teste1[20367:207] i2 8
is this right?
then when i press the iphone home button to close the app, dealloc methos nevers get called, i believe because viewController's retainCount.... i am right?
This is the apple's view application template...should work ...doesnt it?
Can someone explain me why the initial retaincount is 2????? and the other 8??? the xib 开发者_如何学Gofile is blank....
You don't have to worry about actual retain count of your object as long as you are following Memory Management rules. The framework may retain or release the objects so the retain count will be changed out of your control.
You may not get the dealloc called when terminating the app. This is not the way to test. And root view controller is always present in the app. You should be concerned about other classes which you have created yourself and be sure that their dealloc is getting called when they are meant to be called (not in the time of termination). And also don't rely on retainCount. Apple recommends to use it nowhere.
Say you have two views and corresponding view controllers other than root view controller. In 1st view you have created other objects. Now when switching to 2nd view, you should free up the memory for 1st view if they are not necessary. In this case you should test that the objects for 1st view are really been deallocated. If you have coded in this way but they are are not getting called, then there must be a leak. But yes, you should not test this upon termination of the app.
精彩评论