SARANGA
SARANGA

Reputation: 172

ARC - popviewController/DismissViewController

I have some doubts with the ARC.

In ARC: When a viewcontroller is dismissed by using the dismissViewController/popViewController, Will it's allocated memory automatically get released?

What will happen when there is an asynchronous NSURLConnection is running and before that operation completed, viewcontroller got dismissed?

Thank you

Upvotes: 1

Views: 524

Answers (2)

rdelmar
rdelmar

Reputation: 104092

When you dismiss a view controller (or pop it), it will be deallocated if you didn't make any strong pointers to it (that controller is retained by the navigation controller, or the presenting view controller, so you usually don't need to have a pointer to it when you create it and push or present it).

If an NSURLConnection is in progress when you dismiss the controller, and that controller is the delegate of the connection (which is the usual case), then the controller will not be deallocated until connectionDidFinishLoading or connectionDidFailWithError is completed. This is because the connection is still alive, and it has a strong pointer to its delegate.

Upvotes: 1

Aardvark
Aardvark

Reputation: 598

Dismissviewcontroller will stop the view controller from being displayed. The actual view controller will only be removed from memory under ARC when the actual variable referring to the view controller is out of scope. E.g.

ViewControllerType *vc = [[ViewControllerType alloc] init]; // Create it
[self presentViewController:vc animated:TRUE completion:Nil]; // Present it


vc = Nil; // Destroy it, or the method vc was declared inside is out of scope does the same

Typically you wouldn't do vc = Nil while vc is still displayed

If you allow the ViewController to dismiss prior to receiving a response for your NSURLConnection you should deal inside the response thread of the NSURLConnection for the condition that the ViewController is no longer there. Remember, however that the ViewController may still be in scope. It's really a question that can only be answered with a closer look at the use case and associated code.

Upvotes: 0

Related Questions