CoreData leak when reading a property
I have the following code in a loop iterating over the different document
objects:
NSAutoreleasePool*开发者_开发技巧 pool = [[NSAutoreleasePool alloc] init];
NSData* data = [document primitiveValueForKey:@"data"];
[document.managedObjectContext refreshObject:document mergeChanges:NO];
[pool release];
The "data" property is a large blob (a 1MB image). And as I monitor the memory with the Allocation Instrument memory usage is increasing. I cannot find where the leak is coming from and how to remove it.
Thanks!
Something is wrong with your sample code, did you mean:
NSData *data = [document primitiveValueForKey:@"data"];
As data is currently not assigned within the scope of your autoreleasepool it is also not released with with your autoreleasepool
Why are you using primitiveValueForKey and not a dynamic accessor?
The dynamic accessors are much more efficient, and allow for compile-time checking.
How about calling [pool drain]
instead of [pool release]
?
I managed to solve the problem by doing : [document.managedObjectContext processPendingChanges]
right before draining the pool. However, I don't understand what pending changes would be there? Could someone enlighten me on that?
Your observation that processPendingChanges
seems to solve the problem suggests to me that, as you import, the UndoManager for your NSManagedObjectContext is keeping track of all the changes you make as you do your bulk import.
What processPendingChanges
is doing (as I understand it) is pushing changes stored in the managedObjectContext to the persistent store.
Try [[document managedObjectContext] setUndoManager:nil]
(or create a new managedObjectContext for the import and set its undoManager to nil, if your document.managedObjectContext
is the 'main' managedObjectContext and you don't want to turn off undo registration.
精彩评论