small medium large xlarge

Back to: All Forums  Core Data
Generic-user-small
07 Oct 2011, 23:36
James Testa (1 post)

In XCode when I run Build and Analyze on the DocumentPreferences.m file in the CDPreferences folder I get the following potential memory leak.

Is this a real memory leak? If so, how can I fix it?


DocumentPreferences.m:97:3 Potential leak of an object allocated on line 90 and stored into ‘request’ DocumentPreferences.m:90:28 Method returns an Objective-C object with a +1 retain count (owning reference) DocumentPreferences.m:97:3 Object allocated on line 90 and stored into ‘request’ is no longer referenced after this point and has a retain count of +1 (object leaked)


in the following method

- (NSManagedObject*)findParameter:(NSString*)name;
{
	NSManagedObjectContext *moc;
	NSManagedObject *param;
	NSError *error = nil;
	moc = [[self associatedDocument] managedObjectContext];
	NSFetchRequest *request = [[NSFetchRequest alloc] init];
	[request setEntity:[NSEntityDescription entityForName:@"Parameter"
								   inManagedObjectContext:moc]];
	[request setPredicate:[NSPredicate predicateWithFormat:@"name == %@", name]];
	
	param = [[moc executeFetchRequest:request error:&error] lastObject];
	if (error) {
		NSLog(@"%@:%s Error fetching parameter: %@", [self class], _cmd, error);
		return nil;
	}
	[request release], request = nil;
	return param;
}
Avatarsmall_pragsmall
11 Dec 2011, 20:40
Marcus S. Zarra (284 posts)

The warning, while technically accurate, is not something you will see. The leak would only happen if there was an error in the fetch.

To remove the warning, change the conditional:

if (error && !param) {
  NSLog(@"%@:%s Error fetching parameter: %@", [self class], _cmd, error);
  [request release], request = nil;
  return nil;
}
You must be logged in to comment