1
votes

I have a problem on the memory management of a tableviewcell (exc_bad_access).
One of mine tableviewcell include a uiwebview that loads asynchronous html data (loadHTMLString method) and the current controller is set as its delegate.
When i pop tableviewcontroller from my navigation controller too fast the app crashes.

In my opinion this is the reason why it fails:
- if cell deallocs before the controller => all ok, delegate is still live and i can even set its delegate to nil in its own dealloc method
- if cell deallocs after the controller (i guess because table view cells are autoreleased) => the app crashes because its delegate it's still set to the deallocated controller

Any idea how to correctly solve this?? Thank you..

CODE in cellForRowAtIndexPath:

...
CustomTableViewCell * cCell = (CustomTableViewCell*)cell; 
cCell.myWebView.delegate = self; 
[[cCell myWebView] loadHTMLString:html baseURL:baseURL];
....
2
Could you give a code snippet? - fvwmer
TO check your guess you can add NSLog at cell and viewController classes dealloc, i'm sure autoreleased cell is deallocated before the controller. - A-Live
App crashes before NSLOG in the cell dealloc method is called. I guess autorelease pool comes after controller is deallocated. Obviously i tap back button really really fast as controller is pushed. If i comment "[[cCell myWebView] loadHTMLString:html baseURL:baseURL];" everything works fine.. - user1427835
Post the code for the cell. Something else is amiss here. - futureelite7

2 Answers

1
votes

Try to add stopLoading at cell deallocation before releasing the web view.

0
votes

This is an older question, but it never really got solved, and I just ran into it myself.

This happened to me: The view controller would crash when a cell was loaded with a web view who had the cell as it's delegate. UIWebView's documentation states that a web view's delegate should be set to nil before it is deallocated. This seems to also be true for reused table view cells.

My solution: override these in your custom cell:

- (void)prepareForReuse
    {
    [super prepareForReuse];
    self.myWebView.delegate = nil;
    [self.myWebView stopLoading];
    }

- (void)dealloc
    {
    self.myWebView.delegate = nil;
    [self.myWebView stopLoading];
    }

This fixed it for me.