2013-05-11 55 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

相关问题