Why is the autoreleased AND box released in iOS 6 apps?

I haven't done iOS development since iOS 3, so my memory is a little hazy (although memory management was never something I struggled with and my mind is perfectly clear).

I'm starting a new project and I don't understand why the skeleton code is structured like this:

- (void)dealloc
{
    [_window release];
    [super dealloc];
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[[UIWindow alloc]
                    initWithFrame:[[UIScreen mainScreen] bounds]]
                   autorelease];
    // ... snip ...
}

      

  • Why will the window object be auto-realized? I'm pretty sure this was never the case in older versions of iOS.
  • Where from _window

    ? Is this just another way to access [self window]

    ?

I would write it like:

- (void)dealloc
{
    [self.window release];
    [super dealloc];
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc]
                   initWithFrame:[[UIScreen mainScreen] bounds]];
    // ... snip ...
}

      

I've always been drummed to never release an auto-implemented object, and in fact this usually results in a segmentation fault.

+3


source to share


4 answers


In your second example, you will pass the window object as it alloc

will give the object a hold value of 1, you assigned it _window

via a property that will also keep the object assigned to it.

True, you shouldn't let go of the auto-update object, but in dealloc, you free the iVar for the property window

. You should always release any property declared as retained or strong. (although not when using ARC). _window

is now automatically generated as iVar for property window

.

Some people think that you shouldn't use properties self.

in init

or dealloc

. So I do this:

 [_window release], _window = nil;

      



This wil set _window

to nil after freeing it, making sure that if any other thread might want to use it, it will call nil

. This could have prevented the crash, but it might have created some strange behavior. It's completely up to you.


You should go to ARC, which is the flexibility feature to add release / autoload to compiletime for you. You don't need to add them yourself if you've configured the property correctly when using ARC.

+2


source


So I figured it out (mostly). The ad @property

has an attribute strong

. Apparently this is new to ARC (which I don't actually use), and otherwise it is just an alias for retain

, therefore self.window = [ ... ]

retains the object, hence auto-advertised.



Still not clear on the variable _window

, but I'm guessing it's just a shortcut.

0


source


Check what the memory descriptor looks like window

. I assume it is strong

/ retain

, in which case when you set the property window

that value is retained, so it needs to be freed in dealloc

.

Following your path to code.

  • [UIWindow alloc]

    = saveCount of 1.
  • autoreleasing = keepCount 0
  • setting self.window allows you to shrink keepCount, which is now = 1, and until ...
  • in dealloc

    you release it so saveCount = 0 and the object will be deleted

You probably missed that in subsequent iOS SDKs, automatically synthesized properties automatically create instance variables with underscore prefixes. So you can do self.window = nil

in dealloc

.

0


source


1. Why will the window object be auto-realized? I'm pretty sure this was never the case in older versions of iOS.

If the window object is not auto-implemented, it will leak. Since you are using

 self.window  = [[[UIWindow alloc] ....]autorelease];

      

Syntax

self.

allows you to call a setter function that stores the object once. So the actual window object that we allocated with [UIWindow alloc]

hence should be released autorelease

. Saving using syntax is self.

conceptually released in dealloc. So for one selection plus one hold, we release twice.

Where does _window come from? This is another way to access [self window]

.

Ok, this question has already been discussed here

0


source







All Articles