views:

90

answers:

2

Hi,

Here is the code I have:

Phone SDK undestanding cocoa object live cycle:

- (void) DismissWelcomeMessage: (UIAlertView *) view
{
    [view dismissWithClickedButtonIndex:0 animated:YES];
}

- (void) ShowWelcomeMessage 
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Blah" message:@"Blah Blah" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alert show];
    [self performSelector:@selector (DismissWelcomeMessage:) withObject: alert  afterDelay: WELCOME_MESSAGE_DELAY]; 

    [alert release];
}

ShowWelcomeMessage is called first.

Why DissmissWelcomeMessage works fine and does not crash even though alert object is released?

Is because Dismiss function uses copy of the object passed on stack as a parameter when function? But even then would not it be just a copy of the pointer pointed to the now deallocated object?

Or [alert release] just decriment reference counting and does not really do the same as delete in C++?

A: 

It's possible that performSelector is retaining the object passed in, which is why it's still valid when DismissWelcomeMessage is called.

kprevas
+3  A: 

performSelector retains the object, thus your release doesn't cause its retain count to go to zero.

See NSObject docs

This method retains the receiver and the anArgument parameter until after the selector is performed.

nall
Thank you, I missed that part in the NSObject docs!
leon