2013-05-11 76 views
0

當我覆蓋我的項目時,我得到很多潛在的對象泄漏。當我嘗試釋放該對象時,出現錯誤'someobject發送到釋放實例'釋放分配的對象(方法保留具有+1保留計數的objective-c對象)

我無法理解在哪裏完美釋放對象。 我需要支持上述的iOS 4.3 .Gone通過谷歌的版本,發現ARC從IOS 5

- (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]; 
    [email protected]"Options"; 
    LogOut *logout=[[LogOut alloc]initWithNibName:@"LogOut~iPad" bundle:nil]; // method retains objective-c object with +1 retain count 
    [email protected]"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啓用錯誤

我無法理解何時釋放對象。

回答

-1

,如果你寫

self.tabBarController = [[[UITabBarController alloc] init] autorelease]; 

,然後手動釋放這種情況下,例如[self.tabBarController發佈];那麼它總是崩潰。

,你也寫在你的問題

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]; 
     } 
+0

這也不正確。請參閱我的回答 – 2013-05-11 07:23:15

0

所有ARC首先是IOS 4.0後支持。根據蘋果的文檔..

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]; 
    [email protected]"Options"; 
    LogOut *logout=[[LogOut alloc]initWithNibName:@"LogOut~iPad" bundle:nil]; // method retains objective-c object with +1 retain count 
    [email protected]"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; 

你不需要顯式地釋放標籤欄控制器,因爲你已經把autorelease放在它上面了..你應該在dealloc中寫[tabBarController release];釋放與物業相關的伊娃。

看着你問的問題..我幾乎肯定你不明白屬性和實例變量的關係。請找到你自己一個關於這些以及關於ios的內存管理的好教程。

希望這有助於。

+0

感謝您的回答。現在我已將我的項目轉換爲在「編輯 - >重構」中啓用的ARC。是否要刪除所有發佈聲明? – Honey 2013-05-11 10:02:47

+0

嘿,你能告訴我什麼時候可以說autorelease,因爲如果我說autorelease有時我得到error.Message發送到釋放實例。 – Honey 2013-05-11 10:12:29

+0

當我嘗試我們的代碼時,我得到這樣的錯誤[UITabBarController performSelector:withObject:withObject:]:發送到釋放實例的消息 – Honey 2013-05-11 10:20:38

相關問題