简体   繁体   中英

Release allocated objects(Method retains objective-c object with +1 retain count)

When I anylayze my project I get a lot of Potential leaks of objects.When I try to release that object I get error 'someobject send to deallocated instance'

I couldnot understand where to release the objects perfectly. I need to support the versions above ios 4.3 .Gone through google and found that ARC is enabled from 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; 
 }

When I write

[self.tabBarController release];
[searchController release];
[optionview release]; 
[logout release];

after
[self.window setFrame:rect];
I get Bad Excess Error

I couldnot understand when to release objects.

First of all ARC is supported after ios 4.0 .. According to 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.

Source: http://developer.apple.com/library/ios/#releasenotes/ObjectiveC/RN-TransitioningToARC/Introduction/Introduction.html

and this is what your code should look like..

   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; 

you do not need to to explicitly release the tab bar controller as you have put autorelease on it.. you should though in the dealloc write [tabBarController release]; to release the ivar associated with the properties.

Looking at the question you have asked .. I am pretty much certain that you do not understand properties and instance variable relation. Please find your self a good tutorial about these and as well as about Memory management for ios.

Hoping this helps.

if you write

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

and then manually release this instance like [self.tabBarController release]; then it always crash.

and you also write this in your question

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

tabBarController is autorelease and then you try to release its viewcontrollers?

try this:

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


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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM