I'm having a memory leak in the following code. I got 87.5% of the memory leak before Instruments itself crashed. Maybe one of you can tell me what is wrong with this code. Do I need to be releasing anything in here? Thanks.
- (void)browseSSviewControllerDidFinish:(browseSSviewController *)controller {
<SNIP>
MANAGED_OBJECT_1 = [self newFormatFromFormat:MANAGED_OBJECT_2];
<SNIP>
}
- (Format *)newFormatFromFormat:(Format *)formatToCopy {
NSLog(@"making new format that's a copy");
Format *thisNewFormat = [self newBlankFormat];
[self updateFormat:thisNewFormat withNumbersFromFormat:formatToCopy];
return thisNewFormat;
}
-(void)updateFormat:(Format *)formatToCopyTo withNumbersFromFormat:(Format *)formatToCopyFrom {
NSLog(@"copying formats");
formatToCopyTo.x = formatToCopyFrom.x;
formatToCopyTo.y = formatToCopyFrom.y;
formatToCopyTo.z = formatToCopyFrom.z;
formatToCopyTo.a = formatToCopyFrom.a;
formatToCopyTo.n = formatToCopyFrom.n;
formatToCopyTo.u = formatToCopyFrom.u;
formatToCopyTo.s = formatToCopyFrom.s;
}
- (Format *)newBlankFormat {
NSLog(@"making new blank format");
gfghfAppDelegate *del = (gfghfAppDelegate *)[UIApplication sharedApplication].delegate;
NSManagedObjectContext *MOC = del.managedObjectContext;
NSPersistentStoreCoordinator *PSC = [MOC persistentStoreCoordinator];
NSManagedObjectModel *MOM = [PSC managedObjectModel];
NSEntityDescriptio开发者_C百科n *entity = [[MOM entitiesByName] objectForKey:@"Format"];
Format *thisNewFormat = [[NSManagedObject alloc] initWithEntity:entity insertIntoManagedObjectContext:MOC];
thisNewFormat.slot = [NSNumber numberWithInt:-1];
NSLog(@"slot = %@",thisNewFormat.slot);
return thisNewFormat;
}
Here is the 87.5% of the leak that I was able to get out of my backtrace before Instruments crashed:
+0xc7 calll DYLD-STUB$$objc_msgSend = 87.5%
I didn't use mutableCopy to copy my MANAGED_OBJECTs because it just didn't work.
I rewrote the newBlankFormat:thisNewFormat method and used the "insertNewObjectForEntityForName:inManagedObjectContext:" convenience method of NSEntityDescription (see "Creating, Initializing, and Saving a Managed Object" in the "Creating and Deleting Managed Objects" chapter of Apple's "Core Data Programming Guide").
I think my problem initially that I was using "NSManagedObject alloc" with "initWithEntity:insertIntoManagedObjectContext", which does not make an auto-released object. It would leak even when I would release it myself from another method using a "release" later on the returned value using the instance variable that got the return value from this method. However, whether or not I put in a "retain" around the malloc, the release count wasn't being incremented I don't believe, since it's not a proper init function? I'm not sure why.
But I don't care because the following revision fixed the leak, and as it turns out, is what Apple recommended to use anyway (I think I was using the old code from an example project or something).
Here is the revision that appears to have fixed the leak, in the new version of the "newBlankFormat:thisNewFormat" function:
- (Format *)newBlankFormat:(Format *)thisNewFormat {
NSLog(@"making new blank format");
gfghfAppDelegate *del = (gfghfAppDelegate *)[UIApplication sharedApplication].delegate;
NSManagedObjectContext *MOC = del.managedObjectContext;
//[MOC reset]; don't do this, it will erase MANAGED_OBJECT_2
thisNewFormat = [NSEntityDescription insertNewObjectForEntityForName:@"Format" inManagedObjectContext:MOC]; //this is much better than using the malloc variety
thisNewFormat.slot = [NSNumber numberWithInt:-1];
NSLog(@"slot = %@",thisNewFormat.slot);
return thisNewFormat;
}
精彩评论