I've the class OTNetwork that is subclass of UIViewController.
When user pushes a button I use this code to call it:
OTNetwork *net = [[OTNetwork alloc] initWithNibName:@"OTNetwork" bundle:nil];
[self presentModalViewController:net animated:YES];
[net release];
When user wants to exit, pushes a button and the OTNetwork object sends a notification that makes the caller ViewController dismiss the view controller. This is the code:
[self dismissModalViewControllerAnimated:YES];
My problem is that the OTNetwork object dealloc method is never called. And here is the invalidate call to a timer that never is stopped. An aditional problem is the memory leak.
In the caller View Controller this object only is created and dismissed by these lines of code.
Any help please?
Thanks in advance!.
Autorelease never guarantees when the dealloc will be called and you shouldn't rely on that.
And autorelease pools should be used for threads or when you have large memory allocations in a closed loop. It shouldn't be used on the main thread which already runs in a separate pool.
You should probably move the invalidate timer call to viewDidUnload or viewWillDisappear in OTNetwork class.
Hope that helps.
[Update: Mar 02, 2012]
If you'd like to ensure that dealloc is called, try the following
1) Store a reference to OTNetwork controller
OTNetwork *net = [[OTNetwork alloc] initWithNibName: @"OTNetwork" bundle: nil];
net.delegate = self;
self.modalV = net; // @property (nonatomic, strong) OTNetwork *modalV;
[net release];
[self presentModalViewController: modalV animated: YES];
2) Define a protocol / delegate in OTNetwork to report back when it's closed
// .h
@protocol OTNetworkDelegate;
- (void) netViewClosed;
@end
// .m
- (void) viewDidUnload
{
[self.delegate netViewClosed];
}
3) In mainViewController, implement the protocol
- (void) netViewClosed
{
if(modalV)
{
[modalV release], modalV = nil;
}
}