iOS Core Data - unexpected memory leak
I have Multi-threaded core data setup - it works very well. In this setup I have a series of functions that grab required data and so forth. They all work well except for this one which is causing a small leak - but it adds up quite fast and crashes the app.
- (id)getRecordOfType:(NSString *)type WithCode:(NSString *)codeString{
id returnObj = nil;
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:[NSE开发者_如何转开发ntityDescription entityForName:type inManagedObjectContext:self.managedObjectContext]];
NSPredicate *categoryPred = [NSPredicate predicateWithFormat:[@"code='" stringByAppendingFormat:@"%@'", codeString]];
[request setPredicate:categoryPred];
categoryPred = nil;
if (![request entity]) {
[request release]; request = nil;
return nil;
}
NSError *error;
NSMutableArray *results = [[self.managedObjectContext executeFetchRequest:request error:&error] mutableCopy];
for (id tmpObj in results) {
if([(NSString *)[tmpObj code] isEqualToString:codeString])
returnObj = tmpObj;
break;
}
[results release];
[request release];
return returnObj;
}
If anyone can shed some light here it would be greatly appreciated.
Thanks in advance,
Michael
I cannot spot easily a memory leak. On the other hand, I think that a memory leak is not easily the cause of a crash, only in case you run out of memory. Anyway, it seems to me that there is a problem with your code that could possibly be causing the crash:
from results,
NSMutableArray *results = [[self.managedObjectContext executeFetchRequest:request error:&error] mutableCopy];
you select an object:
returnObj = tmpObj;
and the return it after releasing results:
[results release];
...
return returnObj;
It is my understanding that releasing an array will also release all of its object, so you are returning an object which could be deallocated at any moment (or even just deallocated).
精彩评论