First, in the viewDidLoad root view controllers, I initialize an NSDictionary with NSManagedObjects arrays, for example:
- (void)viewDidLoad { [super viewDidLoad]; self.title = @"Decks"; UIBarButtonItem *browseButton = [[UIBarButtonItem alloc] initWithTitle:@"Browse" style:UIBarButtonItemStylePlain target:self action:@selector(loadBrowseView)]; self.navigationItem.rightBarButtonItem = browseButton; [browseButton release]; NSError *error = nil; if (![[self fetchedResultsController] performFetch:&error]) { } self.categories = [fetchedResultsController fetchedObjects]; NSMutableDictionary *tipsMutableDictionary = [[NSMutableDictionary alloc] init]; for (Category *category in self.categories) { NSMutableArray *tipsToSort = [NSMutableArray arrayWithArray:[[category valueForKey:@"tips"] allObjects]]; NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending: YES]; [tipsToSort sortUsingDescriptors: [NSArray arrayWithObject: sortDescriptor]]; [sortDescriptor release]; [tipsMutableDictionary setObject:[NSArray arrayWithArray:tipsToSort] forKey:[category name]]; } self.tips = tipsMutableDictionary; [tipsMutableDictionary release]; }
Now "self.tips", which is a saved NSDictionary property on my root view controller, is configured to host NSManagedObjects arrays. I gladly continue to use this data for about 5 minutes (including passing it to other properties saved in view mode) until I receive the following message:
malloc: *** error for object 0x3b300b0: double free
At this point, after an error, but before the actual failure, the backtrace looks like this:
#0 0x98152072 in malloc_error_break ()
In particular, my code is not there.
The print object at 0x3b300b0 shows:
<Tip: 0x3b300b0> (entity: Tip; id: 0x3b2ffe0 <x-coredata://10B4E6EE-ACF3-4316-AE10-6E06E8FFFF46/Tip/p9> ; data: <fault>)
And the most interesting, shell malloc_history at this address:
ALLOC 0x3b300b0-0x3b300ef [size=64]: thread_a0a3f500 |start | main | UIApplicationMain | -[UIApplication _run] | CFRunLoopRunInMode | CFRunLoopRunSpecific | PurpleEventCallback | _UIApplicationHandleEvent | -[UIApplication sendEvent:] | -[UIApplication handleEvent:withNewEvent:] | -[UIApplication _reportAppLaunchFinished] | CA::Transaction::commit() | CA::Context::commit_transaction(CA::Transaction*) | CALayerLayoutIfNeeded | -[CALayer layoutSublayers] | -[UILayoutContainerView layoutSubviews] | -[UINavigationController _startDeferredTransitionIfNeeded] | -[UINavigationController _startTransition:fromViewController:toViewController:] | -[UINavigationController _layoutViewController:] | -[UINavigationController _computeAndApplyScrollContentInsetDeltaForViewController:] | -[UIViewController contentScrollView] | -[UIViewController view] | -[RootViewController viewDidLoad] | -[_NSFaultingMutableSet allObjects] | -[_NSFaultingMutableSet willRead] | -[NSManagedObjectContext(_NSInternalAdditions) _retainedObjectWithID:optionalHandler:withInlineStorage:] | +[NSManagedObject(_PFDynamicAccessorsAndPropertySupport) allocWithEntity:] | _PFAllocateObject | malloc_zone_calloc ---- FREE 0x3b300b0-0x3b300ef [size=64]: thread_a0a3f500 |start | main | UIApplicationMain | GSEventRun | GSEventRunModal | CFRunLoopRunInMode | CFRunLoopRunSpecific | __CFRunLoopDoObservers | _performRunLoopAction | -[_PFManagedObjectReferenceQueue _processReferenceQueue:] | -[NSManagedObjectContext(_NSInternalAdditions) _forgetObject:propagateToObjectStore:] | -[NSManagedObjectContext(_NSInternalAdditions) _forgetObject:propagateToObjectStore:removeFromRegistry:] | CFDictionaryRemoveValue | CFRelease | _PFDeallocateObject | malloc_zone_free ALLOC 0x3b300b0-0x3b300cf [size=32]: thread_a0a3f500 |start | main | UIApplicationMain | GSEventRun | GSEventRunModal | CFRunLoopRunInMode | CFRunLoopRunSpecific | __CFRunLoopDoObservers | _performRunLoopAction | -[_PFManagedObjectReferenceQueue _processReferenceQueue:] | _PFDeallocateObject | free_tiny_botch | szone_error | start | _NSPrintForDebugger | -[NSManagedObject description] | +[NSString stringWithFormat:] | -[NSPlaceholderString initWithFormat:locale:arguments:] | _CFStringCreateWithFormatAndArgumentsAux | CFStringCreateMutable | _CFRuntimeCreateInstance | malloc_zone_malloc
My interpretation of the above says that “[RootViewController viewDidLoad]” in the first ALLOC statement is that I first populated NSDictionary hints with data. Then, following the FREE statement, main.m decides to free these objects, even if they are in a saved NSDictionary, for which I am responsible for releasing?
EDIT Upon request, the output of NSZombieEnabled is output here:
*** -[CFString retain]: message sent to deallocated instance 0x3b300b0
Refund after that:
#0 0x01d6a3a7 in ___forwarding___ ()
The indicated line of code above (/Users/***/RootViewController.m:32):
NSArray *tipsArray = [NSArray arrayWithArray:[self.tips objectForKey:categoryNameString]];
All I can say from this is that the objects I'm trying to get from "self.tips" have already been released (by whom and why?)