0

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

我无法理解在哪里完美地释放对象。我需要支持ios 4.3以上的版本。通过google发现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];
    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错误之后

我无法理解何时释放对象。

4

2 回答 2

0

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

希望这会有所帮助。

于 2013-05-11T07:22:10.210 回答
-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 是自动释放的,然后你尝试释放它的视图控制器?

尝试这个:

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


       -(void)dealloc {
            //release your all objects
             [super dealloc];
        }
于 2013-05-11T06:10:23.383 回答