释放已分配的对象(Method保留具有+1保留计数的objective-c对象)

时间:2013-05-11 05:38:52

标签: iphone ios objective-c xcode4.5

当我执行任何项目时,我会遇到很多潜在的对象泄漏。当我尝试释放该对象时,我得到错误'someobject send to deallocated instance'

我无法理解完美释放物体的位置。 我需要支持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错误

我无法理解何时发布对象。

2 个答案:

答案 0 :(得分:0)

首先在ios 4.0之后支持ARC ..根据apple docs ..

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的内存管理的好教程。

希望这有帮助。

答案 1 :(得分:-1)

如果你写

   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是autorelease,然后你尝试发布它的viewcontrollers?

试试这个:

       self.tabBarController = [[UITabBarController alloc] init];
      self.tabBarController.viewControllers = [NSArray arrayWithObjects:searchController, optionview,logout, nil];


       -(void)dealloc {
            //release your all objects
             [super dealloc];
        }