views:

209

answers:

2

I am new to Iphone apps and I am trying to build a tab based app. I am attempting to have a table ontop of an image in both tabs. On tab with a table of audio links and the other tab with a table of video links.

This has all gone swimmingly, I have created two viewControllers for the two tables. All the code works great apart from to get it to work I have to comment out the super dealloc in the - (void)dealloc {} in the videoTableViewController for the second tab.

If I don't I get the error message:

FREED(id): message numberOfSectionsInTableView: sent to freed object

please help, i have no idea why it is doing this...

+1  A: 

If you're not calling [super dealloc], then your object isn't being deallocated and you're leaking memory. You need to uncomment that call to [super dealloc].

The exception you note above means that a table view is still trying to access your deallocated object as a data source. This is the problem you need to solve. Presumably, the table view that's making this call is owned by the view controller that's been deallocated.

If your view controller is not a subclass of UITableViewController, then you will need to release the table view reference you're holding. If it is a subclass of UITableViewController, then there must be some other place where that table view is being retained where it shouldn't be.

Alex
Brilliant yes agreed alex. So i have a uview class audio with a table view inside it controlled by a UITableView class audioTableViewController.The same setup for the videos. I assume therefore that I need to release the table view I am referencing do I do this in the dealloc method of the audioTableViewController?thanks
padatronic
If this is not a subclass of `UITableViewController`, then yes, you need to release the table view. If you allocated it (even by loading a NIB) you're responsible for releasing it.
Alex
A: 

UITableViewController handles memory management for its own tableView object, so you theoretically don't have to worry about it.

As Alex said, it's definitely bad to not call [super dealloc]; it is a memory leak to leave that out.

To solve this, you need to figure out why your table view is living longer than your view controller (that's what's giving you that FREED(id) error). One guess is that you're leaving the view in the view hierarchy but releasing the controller. The desired behavior when you're moving away from one view controller to another is to remove the old view from the view hierarchy by calling [oldView removeFromSuperview] and then releasing the view controller, if you want to release it at all. The reason removeFromSuperview is important is that superviews always retain their subviews, so this could be why your tableView is so undead.

If your app is not memory intensive, it may be easiest to simply keep your view controllers allocated (not release them) -- that would solve your problem, although it is not really addressing the issue that your table view is living longer than expected, which may be a symptom of some inefficient code that would be nice to clean up.

Tyler
Hi chaps, thanks for the help so far. Your answers make a lot of sense to me but i still can't get it to work. I have restructured the project to try and solve my problem currently the tab loads an audio.xib with audioViewController and the same structure for video.in audioViewController.h I add a UITableView as an IBOutlet, eg@interface audioViewController : UIViewController { IBOutlet UITableView *audioTable;}
padatronic
I then in IB add a class of auidoTableViewController to the audio.xib and delete the table from it. Add a table to the view in IB and link it to the audioTable i created in audioViewController.h. I link the datasource and delegate to the audioTableViewController in IB.I then add to audioViewController.m- (void)viewDidUnload { // Release any retained subviews of the main view. // e.g. self.myOutlet = nil; [audioTable removeFromSuperview];}- (void)dealloc { [audioTable release]; [super dealloc];}.
padatronic
I do the same for the video and when i try to change tabs it bails but now no error is registered in console. Is this a correct way to structure my app and am I trying to release the audioTable view in the wrong place?hope this is clear, having a real faff with this. Not sure how to detect what is wrong
padatronic
A table controller comes with its own table, and that's the one it is designed to work with. Check out the docs:http://developer.apple.com/iphone/library/documentation/UIKit/Reference/UITableViewController_Class/Reference/Reference.html#//apple_ref/occ/cl/UITableViewControllerhttp://developer.apple.com/iphone/library/documentation/UserExperience/Conceptual/TableView_iPhone/Introduction/Introduction.html
Tyler
great, thanks so much for your help, got it working. I added IBOutlet for tableView and tableViewController in my main view controller, so then I could release them there. I've gone a bit around the houses but thanks so much for the help
padatronic