Parent ViewController deallocated upon memory warning - iphone

I have a MainViewController, from which a new VideoPageViewController is presented modally.
In the ViewPageViewController, it will load a web page and launch the video, which is a heavy operation and sometimes causes memory warning. When it is ready to return to the MainViewController by dismissModalViewController, it says the MainViewController is already deallocated! The app thus crashes.
This happens sometimes, but not always.
Is there any exception handling I can take on it? Can I recreate the parent view controller? HELP!
Thanks

Just retain the MainViewController so it's not released when that happens.
You can probably do that in your application delegate, or in the class that owns it.
Remember to release it when/if you are done using it, so it's properly disposed of.

Related

Issue with view controller not releasing memory

I'm working on an app where different view controllers get pushed and dismissed via dismissModalViewControllerAnimated.
I'm having some memory issues with the app just crashing after a while. Looking at the Leaks instrument, I see that my overall allocations keeps going up and up. Even after the viewcontroller is dismissed, memory does not go down.
Are there any obvious reasons for this? What is the simplest and easiest way to find out why my app is crashing? Thanks
POSSIBLE SOLUTIONS
I went through some trial and error as well as googling and made a few changes:
1) A delegate relationship may have been retaining the viewController, so I changed the object's delegate property to weak.
2) NSTimer's should be invalidated before dismissing viewController.
3) UIView animations may interfere with dealloc being called? You can use [view.layer removeAllAnimations] to end them before popping your viewController.
If your memory is not go down after the dismissModalViewControllerAnimated .. it means you are creating the Global Object of the ViewController And after dismissing you are not setting the Object = nil;
If you set nil then your memory goes down automatically.

iPhone, why and how do i correct these anlyzer warnings while retaining a view controller?

I've struggled to get a login / startup view to show before my mainWindow nav controllers and its finally working, but I'm now getting these analyzer warnings. If I release the navigation controller, the release in the dismiss button with cause an error.
what should I do here ?
You get a warning, because you allocate a new object, but your reference is lost when the method returns. You probably want to make your lvc an instance variable, so that you can access it later (and maybe release it when it's no longer needed).

UIViewController is popped from view stack and NSURLConnection crashes the application

I am pushing a UIViewController onto a UINavigationController. This view controller immediately starts a download of an xml feed and then parses it. However, if you hit the back button before it is done downloading, and crashes with EXC_BAD_ACCESS. The line that is crashing it is in parserDidEndDocument and is this line:
if (self.delegate && [self.delegate conformsToProtocol:#protocol(ModelDelegate)]) [self.delegate modelDidFinishParsing:self];
I assume it is crashing because it is trying to access self.delegate which is not assigned anymore. How do I get around this?
Also, I would release the model object in the modelDidFinishParsing method. How would I release this model if it never reaches this method.
I set up objects to handle my downloads (and other asynchronous or long running tasks) in the AppDelegate, then trigger them as required from various controllers. That way they are owned and have persistence through the life of the application.
The best way to do this is to pass them to the viewControllers that will need them (rather than the viewController "expecting" the appDelegate to have such and such an object ready and waiting) - dependency injection.
These objects update my model in some way when they finish and if I need to, I use NSNotifications to announce they are done. This isolates me from the mess I used to get into trying to cancel or swap delegates in viewWillDisappear etc to avoid the kind of issues you are running into.
The reason your app is crashing is probably because NSURLConnection retains its delegate (so it can call back to it reliably) but objects that this delegate has weak references to have been deallocated.
Ie, in your case what self.delegate points to has probably been deallocated when the view controller is popped but the delegate property has not been cleared (set to nil).
The solution to your problem is to clear (nil) self.delegate at the appropriate time when the UIViewController subclass is being popped off the navigation stack.
Note: retaining delegates is not usual behaviour for Cocoa classes. In situations where it happens contrary to standard practice it is documented (see the NSURLConnection docs).

iPhone SDK: Navigation among view controllers causes crash

(Sorry, I restarted this thread as I incorrectly accepted the answer for my problem.)
My app is crashing when I navigate two view controllers in my application. For example, if I do this sequence:
RootController ViewControllerA
ViewControllerB ViewControllerA
My app crashes.
It crashes when I pressed the back button in ViewControllerB. So, It seems like it is with two or more ViewControllers being pushed. Each by themselves work.
I don't know why.
I don't see any output to the console. Is there some type of debugging I should put?
I looked at the dealloc() to make sure all properties were being release before dealloc()
Any ideas?
Thank you.
Is your 2nd view controller holding a reference to the 1st without retaining it? If so, when your 2nd is dealloc'd it could release the 1st and cause it to have no references and be an invalid object.

Removing a UITabBarController's view causes an EXC_BAD_ACCESS exception

I'm trying to remove a UITabBarController from a window in my app by calling removeFromSuperview on the controller's view after adding it to the window previously. However when i do so and after it deallocates all the view controllers that are on the bar successfully, i get an EXC_BAD_ACCESS signal after an autorelease pool is drained. by method swizzling i've found out that it happens after something called UITableViewRowData has its dealloc method called. I'm wondering if there's a bug when the more view controller is attempting to be deallocated. Has anyone else ever run into this problem?
You are most likely releasing something you shouldn't be, can't really say what without some sample code