Reputation: 4301
According to the Instruments I have leaks in the code below. Any nice person that can give me some advice and explanation why i get indications on these lines?
The following lines are tagged as leaks:
NSMutableArray *read_Question = [[NSMutableArray alloc] initWithCapacity: 0];
NSArray *fetchedObjects = [qContext executeFetchRequest:fetchRequest error:&error];
if ([[info valueForKey:@"idQ"] intValue] == questionNr) {
[read_Question addObject:[info valueForKey:@"question"]];
So here is the full code:
- (NSMutableArray *)readQuestion: (int)questionNr {
NSMutableArray *read_Question = [[NSMutableArray alloc] initWithCapacity: 0];
NSError *error;
//=========PREPARE CORE DATA DB===========//
if (managedObjectContext == nil) { managedObjectContext = [(FamQuiz_R0_1AppDelegate *)
[[UIApplication sharedApplication] delegate] managedObjectContext]; }
// Define qContext
NSManagedObjectContext *qContext = [self managedObjectContext];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription
entityForName:@"questions" inManagedObjectContext:qContext];
[fetchRequest setEntity:entity];
NSArray *fetchedObjects = [qContext executeFetchRequest:fetchRequest error:&error];
for (NSManagedObject *info in fetchedObjects) {
if ([[info valueForKey:@"idQ"] intValue] == questionNr) {
[read_Question addObject:[info valueForKey:@"question"]];
[read_Question addObject:[info valueForKey:@"qRightAnswer"]];
[read_Question addObject:[info valueForKey:@"qWrongAnswer1"]];
[read_Question addObject:[info valueForKey:@"qWrongAnswer2"]];
}
}
[fetchRequest release];
[read_Question autorelease];
return read_Question;
}
Upvotes: 0
Views: 153
Reputation: 31161
Are you autoreleasing inside an autorelease pool? i.e. Have you created an instance of NSAutoreleasePool
in the thread calling -read_Question
? I'm assuming this is being called in the main thread, and your main.m
file will by default look like:
int main(int argc, char* argv[]) {
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
int retVal = UIApplicationMain(argc, argv, nil, nil);
[pool drain];
return retVal;
}
I don't think this is your problem, but without having the full details I'll say this anyway -- If -read_Question
is being called in another thread, you need something similar:
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
.
.
.
[self read_Question];
.
.
.
[pool drain];
Otherwise, see @Steven's suggestion!
Upvotes: 0
Reputation: 44886
That's where the objects are allocated, but it's not where they're being leaked. Instruments can tell when an object is allocated, and where it's retained and released, but it has no idea which release corresponds to which retain so it attributes the leak to the initial allocation.
Look for where these objects are used. There's a view in Instruments for showing the history of a block, but you're probably better off thinking this through instead. What code retains these objects? Can you prove that the same code releases them in all cases?
Upvotes: 1