StanLe
StanLe

Reputation: 5137

iPhone App - dismissing modal view controller doesn't dealloc it

I have a UIViewController (call it NumberTwo) which I presented as a modal view controller from another UIViewController (call it NumberOne). NumberTwo contains a touchesBegan method which listens for touches, and it also has an accelerometer method which listens for device orientation changes in the x, y, or z direction. NumberTwo has a button called "Done" which, when tapped, dismisses itself as a modal view controller:

[self dismissModalViewControllerAnimated:NO];

But it seems as though it's still listening for touches, and it's still listening for accelerations. How can I completely free up NumberTwo when I dismiss it? I tried adding a release call as follows:

[self dismissModalViewControllerAnimated:NO];
[self release];

but that caused a EXEC_BAD_ACCESS.

Upvotes: 6

Views: 4479

Answers (3)

Obliquely
Obliquely

Reputation: 7072

Did you release the controller after you presented it? E.g. in your method in NumberOneController that presents it, do you have something like:

NumberTwoController * controller = [NumberTwoController alloc] init];
// do stuff to config controller
[self presentModalViewController: controller];
[controller release];

Unless you want to hang on to NumberTwoController for re-use, this would be the usual pattern. The presentModalViewController method ensures that the controller is retained while it's in use. It should then get tidied up when, within NumberTwoController, you call [self dismissModalViewControllerAnimated: NO].

Upvotes: 3

TPoschel
TPoschel

Reputation: 3872

I had a very similar issue that plagued me for days. It turned out that my view controller class wasn't being deallocated when I dismissed it because that view controller had an active NSTimer that wasn't being invalidated (stopped). I was able to kill the timer in viewDidDisappear.

Upvotes: 3

Greg
Greg

Reputation: 9168

Make sure you are releasing everything you use when you finish with it; The dealloc method is only called when the UIViewController and all of its properties/objects are no longer in use. Never use [self release]; you need to release it from the view controller that created it after you are finished with it.

Upvotes: 1

Related Questions