Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bug with message sent to deallocated UITableViewController object by framework code

I am developing my first iPhone app for a couple of months and now I got stuck on a memory error “message sent to deallocated instance”. I have written down all my observations (from the debugger) but can't find the reason. I will really appreciate any help.

SITUATION:

I have created pick_ListBrowseController, a subclass of UITableViewController. It is called to pick an item from a scrolling list. The objects presented come from a CareData database. I useNSFetchedResultsController.

In the implementation of pick_ListBrowseController, there is a typical method returning a fetched results controller:

- (NSFetchedResultsController *) fetchedResultsController {

  // If the fetched results controller already exists, return it:
     if (fetchedResultsController != nil) { return fetchedResultsController;}   

  (…) // constructing fetchRequest object with entity and sort descriptors

  [NSFetchedResultsController deleteCacheWithName:@"pick_List"];

  // Create fetched request controller:
  NSFetchedResultsController *aFetchedResultsController = 
  [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest 
            managedObjectContext:self.managedObjectContext 
            sectionNameKeyPath:nil cacheName:@"pick_List"]; 
  // Set the fetched results controller's delegate to self;
  //  it will send controllerDidChangeContent:
       [aFetchedResultsController setDelegate: self]; 
  // Assign this new fetched results controller to the property:
   [self setFetchedResultsController: aFetchedResultsController];
  // Return fetched request controller:
  return fetchedResultsController;
}   

BUG SCENARIO A:

STEP 1) I call my pick_ListBrowseController, select an item and navigate back. The view disappears. In the debugger, I can see that the pick_ListBrowseController object had memory address 0x729b950

STEP 2) I call pick_ListBrowseController once again. It is now another instance, with memory address 0x755a4e0

STEP 3) When I try to pick an item, I get error message:

[pick_ListBrowseController controllerDidChangeContent:]: message sent to deallocated instance 0x729b950.

The address is from STEP 1) ! For some reason, the framework code (which is not accessible for me) sends controllerDidChangeContent: to the already deallocated object from STEP 1), not to the object from STEP 2), as it should.

BUG SCENERIO B:

Even worse, it happens even when in STEP 1) I call a different controller object , also a subclass of UITableViewController. Steps 2) and 3) remain the same. The same bug occurs and in the debugger I can see that even now controllerDidChangeContent: is sent to the object from STEP 1). Hence it is sent not only to the wrong (and deallocated) object, it is even sent to an object from a different class!

QUESTIONS:

1) What may be the reason? How to fix it?

2) My app is really big. Is it possible that it became too big causing the memory problems described above? If yes, what can I do to get things right?

Currently, I use Xcode Version 4.2 build 4C199 and I use Automated Reference Counting (ARC) - I went through the refactor process “convert to Objective-C ARC”.

Any help will be highly appreciated.

like image 280
Greg Falda Avatar asked Mar 22 '26 11:03

Greg Falda


1 Answers

It is likely that your FRC is not getting released when your VC is dealloc ed. If it is hanging around and has your VC as a reference to the delegate then you have a dangling pointer. Double check that you are cleaning up the FRC.

like image 107
logancautrell Avatar answered Mar 23 '26 23:03

logancautrell



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!