views:

256

answers:

2

I have a strange (to me) memory leak when accessing an entity in a relationship.

Series and Tiles have an inverse relationship to each other.

// set up the fetch request
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Series" inManagedObjectContext:managedObjectContext];
[fetchRequest setEntity:entity];

// grab all of the series in the core data store
NSError *error = nil;
availableSeries = [[NSArray alloc] initWithArray:[managedObjectContext executeFetchRequest:fetchRequest error:&error]];
[fetchRequest release];

// grab one of the series
Series *currentSeries = [availableSeries objectAtIndex:1];
// load all of the tiles attached to the series through the relationship    
NSArray *myTiles = [currentSeries.tile allObjects];  // 16 byte leak here!

Instruments reports back that the final line has a 16 byte leak cause by NSPlaceHolderString.

Stack trace:

 2 UIKit UIApplicationMain
 3 UIKit -[UIApplication _run]
 4 CoreFoundation CFRunLoopRunInMode
 5 CoreFoundation CFRunLoopRunSpecific
 6 GraphicsServices PurpleEventCallback
 7 UIKit _UIApplicationHandleEvent
 8 UIKit -[UIApplication sendEvent:]
 9 UIKit -[UIApplication handleEvent:withNewEvent:]
10 UIKit -[UIApplication _runWithURL:sourceBundleID:]
11 UIKit -[UIApplication _performInitializationWithURL:sourceBundleID:]
12 Memory -[AppDelegate_Phone application:didFinishLaunchingWithOptions:] /Users/cfish/svnrepo/Memory/src/Memory/iPhone/AppDelegate_Phone.m:49
13 UIKit -[UIViewController view]
14 Memory -[HomeScreenController_Phone viewDidLoad] /Users/cfish/svnrepo/Memory/src/Memory/iPhone/HomeScreenController_Phone.m:58
15 CoreData -[_NSFaultingMutableSet allObjects]
16 CoreData -[_NSFaultingMutableSet willRead]
17 CoreData -[NSFaultHandler retainedFulfillAggregateFaultForObject:andRelationship:withContext:]
18 CoreData -[NSSQLCore retainedRelationshipDataWithSourceID:forRelationship:withContext:]
19 CoreData -[NSSQLCore newFetchedPKsForSourceID:andRelationship:]
20 CoreData -[NSSQLCore rawSQLTextForToManyFaultStatement:stripBindVariables:swapEKPK:]
21 Foundation +[NSString stringWithFormat:]
22 Foundation -[NSPlaceholderString initWithFormat:locale:arguments:]
23 CoreFoundation _CFStringCreateWithFormatAndArgumentsAux
24 CoreFoundation _CFStringAppendFormatAndArgumentsAux
25 Foundation _NSDescriptionWithLocaleFunc
26 CoreFoundation -[NSObject respondsToSelector:]
27 libobjc.A.dylib class_respondsToSelector
28 libobjc.A.dylib lookUpMethod
29 libobjc.A.dylib _cache_addForwardEntry
30 libobjc.A.dylib _malloc_internal

I think I'm missing something obvious but I can't quite figure out what.

Thanks for your help!

Update: I've copied the offending chunk of code to the first part of applicationDidFinishLaunching and it still leaks. Could there be something wrong with my model?

A: 

Do you release availableSeries? Other than that, I don't see anything.

eman
Yeah, it gets released later on. Baffling!
cfihelp
+1  A: 

NSPlaceHolder is a 'singleton'* used by the foundation libraries to defer allocation of an NSSrting's memory in constructs like:

[[NSString alloc] initWithString:@"only know the size of this string in the init, not the alloc"];

If you only ever leak one*, it's not really a leak.

  • I say "singleton" and "one" but there can be multiple NSPlaceHolderStrings - one per NSZone if allocWithZone is used.
Roger Nolan
Interesting! I still don't see anything horribly wrong with what I'm doing so I'll go with this for now. Thanks a lot for your help!
cfihelp