Mukunda
Mukunda

Reputation: 389

Memory Management in ARC

I could not understand how the objects are released in ARC, there is still a confusion going on in this for me.

Suppose i create a view controller or any other using alloc in a method

    -(void) displayView
    {
       RegViewController *sampleView = [[RegViewController alloc] init];
       [sampleView setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
       [sampleView setModalPresentationStyle:UIModalPresentationFormSheet];
       [self presentModalViewController:sampleView animated:YES];
    }

Does it release the object created when the method block completes or we should explicitly release by giving nil to reference?

Upvotes: 1

Views: 187

Answers (4)

Anoop Vaidya
Anoop Vaidya

Reputation: 46533

Does it release the object created when the method block completes or we should explicitly release by giving nil to reference?

Answer is NO.

In the above case sampleView retain count is not reaching 0, as you passed this as an argument to self class. Once all the strong reference will be cleared its retain count will come down to 0, only after that it will get release.

You can simply imagine your code as RegViewController *sampleView = [[[RegViewController alloc] init] autorelease];

Upvotes: 0

D_4_ni
D_4_ni

Reputation: 901

The view controller is automatically released at the end of the block. However this does not mean that it is deallocated - it is still retained by the presentModalViewController:animated: method and will be released (and deallocated) when it is dismissed.

Upvotes: 0

kitschmaster
kitschmaster

Reputation: 1299

in this case sampleView will be released when the modal view is dismissed. one does not need to do anything else in this case.

Upvotes: 1

Dandré
Dandré

Reputation: 2173

ARC stands for Automatic Reference Counting. It takes over the user's responsibility for maintaining object reference counting. That's why you can't call [obj retain] or [obj release] anymore. It releases it for you as soon as the reference counter hits 0. Just remember that it is not a garbage collector. There are cases where this mechanism can cause memory leaks if you're not careful. But in general it works quite well.

Upvotes: 0

Related Questions