views:

284

answers:

4

Greetings. I've got a nav-bar application that's misbehaving. I've got two buttons, one that shows all results from my database and another that shows a subset of my database. Of course, each button has its own action method. Both of these methods instantiate a view controller object of the same class.

If I start the app and only click the "all results" button, I do see all results. The goofy thing is that when I click the button for the subset of results (and see the subset of results), click Back on the nav bar, and then click the first button for the entire set, I see the subset again.

While debugging with break points all over the place, I noticed that the dealloc method of my results view controller doesn't get called. However, when I click Back and then click the all-results button, the alloc/init methods are indeed called again for my results view controller.

So even if I have a blatant memory leak, how is it possible that my freshly allocated/initialized view controller object has the same data as that of the previously instantiated view? Stepping through the code made this problem seem even more bizarre, as it appeared to be behaving properly...just yielding old data.

Any advice at all would be great. Thanks!

+1  A: 

Calling "reloadTableData" on the table view should ensure the data is refreshed. Call that in the action methods.

Nope, that didn't fix it. I called [table reloadData] from the subview's viewWillAppear method, and I also tried it from the action method. :(
That's strange. Are your table delegate methods getting called? So numberOfRowsInSection, cellForIndexPath, etc?
A: 

Why do your buttons repeatedly instantiate the view controller? Why not have pointers to the view controllers as instance variables that you only have to set once and then can use at will?

I tried that too. Still no luck. You'd think that releasing the view controller and reallocating it before pushing it onto the nav controller stack would do away with this weird left-over data anomaly. Ugh.
A: 

This is just a wild guess, but it could be related to the reuse of tableViewCells. Try always creating a UITableViewCell, avoiding the reuse-identifier to see if the old data persists.

Jab
A: 

I figured out what was wrong a while ago and thought I would answer my own stupid question. :)

I had forgotten that I made my Database class a singleton and put a master pointer to "allResults" in the app delegate class.

Thanks anyway for your input. Every little bit helps me understand this new environment better.