[英]iPhone, method returns an objective-c object with a +1 retain count
[英]Release allocated objects(Method retains objective-c object with +1 retain count)
當我對項目進行任何布局時,都會出現很多潛在的對象泄漏。當我嘗試釋放該對象時,我會收到錯誤消息:“某些對象發送到已釋放的實例”
我不明白在哪里完美地釋放對象。 我需要支持ios 4.3以上的版本。通過Google進行搜索,發現從ios 5啟用了ARC。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:
(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
ViewController *searchController=[[ViewController alloc]initWithNibName:@"ViewController_iPad" bundle:nil];
OptionsViewController *optionview=[[OptionsViewController alloc]initWithNibName:@"OptionsViewController~iPad" bundle:nil];
optionview.title=@"Options";
LogOut *logout=[[LogOut alloc]initWithNibName:@"LogOut~iPad" bundle:nil]; // method retains objective-c object with +1 retain count
logout.title=@"Sign Out";
self.tabBarController = [[[UITabBarController alloc] init] autorelease];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:searchController, optionview,logout, nil]; //Object leaked:object allocated and stored into logout is not referenced later in this execution path and has a retain count of +1
[self.window addSubview:tabBarController1.view]; [self.window makeKeyAndVisible];
CGRect rect = [[UIScreen mainScreen] bounds];
[self.window setFrame:rect];
return YES;
}
當我寫
[self.tabBarController release];
[searchController release];
[optionview release];
[logout release];
后
[self.window setFrame:rect];
我收到Bad Excess
錯誤
我不知道何時釋放對象。
首先,ios 4.0之后支持ARC。.根據蘋果文檔..
ARC is supported in Xcode 4.2 for OS X v10.6 and v10.7 (64-bit applications) and for iOS 4 and iOS 5. Weak references are not supported in OS X v10.6 and iOS 4.
來源: http : //developer.apple.com/library/ios/#releasenotes/ObjectiveC/RN-TransitioningToARC/Introduction/Introduction.html
這就是您的代碼的外觀。
ViewController *searchController=[[ViewController alloc]initWithNibName:@"ViewController_iPad" bundle:nil];
OptionsViewController *optionview=[[OptionsViewController alloc]initWithNibName:@"OptionsViewController~iPad" bundle:nil];
optionview.title=@"Options";
LogOut *logout=[[LogOut alloc]initWithNibName:@"LogOut~iPad" bundle:nil]; // method retains objective-c object with +1 retain count
logout.title=@"Sign Out";
self.tabBarController = [[[UITabBarController alloc] init] autorelease];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:searchController, optionview,logout, nil]; //Object leaked:object allocated and stored into logout is not referenced later in this execution path and has a retain count of +1
[self.window addSubview:tabBarController1.view]; [self.window makeKeyAndVisible];
CGRect rect = [[UIScreen mainScreen] bounds];
[self.window setFrame:rect];
[searchController release];
[optionview release];
[logout release];
return YES;
您不需要顯式釋放選項卡欄控制器,因為您已經將其自動釋放了..您應該在dealloc中寫[tabBarController release]; 釋放與屬性關聯的ivar。
查看您所問的問題..我可以肯定地說,您不了解屬性和實例變量關系。 請為您自己找到一個很好的教程,以及有關ios的內存管理的教程。
希望這會有所幫助。
如果你寫
self.tabBarController = [[[UITabBarController alloc] init] autorelease];
然后手動釋放該實例,例如[self.tabBarController release]; 然后它總是崩潰。
你也把這個寫在你的問題中
self.tabBarController = [[[UITabBarController alloc] init] autorelease];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:searchController, optionview,logout, nil];
tabBarController是自動釋放的,然后您嘗試釋放其viewcontrollers?
嘗試這個:
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:searchController, optionview,logout, nil];
-(void)dealloc {
//release your all objects
[super dealloc];
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.