In my TaggingScreen.m init function, I do –
tags = [myTagMgr getMyTags];
In my getMyTags method, I do the following –
NSMutableDictionary *myTags = [NSMutableDictionary new];
....
return myTags;
I get a memory leak for myTags in this method. Where should I release the memory? “tags” is a property which is used throughout the TaggingScreen class. So if I do an autorelease, I get an exception saying “message sent to deallocated instance” when I try to access tags in other methods of the class.
EDIT:
- (NSMutableDictionary *)getMyTags
{
NSMutableDictionary *myTags=[NSMutableDictionary new];
NSFetchRequest *fetchRequest = [[[NSFetchRequest alloc] init]autorelease];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Tag"
inManagedObjectContext:localObjectContext];
[fetchRequest setEntity:entity];
NSError *error = nil;
NSArray *fetchedArrayObjects = [localObjectContext executeFetchRequest:fetchRequest error:&error];
if (fetchedArrayObjects ==nil) {
return nil;
}
if ([fetchedArrayObjects count]==0) {
return nil;
}
Tag *aMessage;
for (int i=0; i<[fetchedArrayObjects count];i++)
{
aMessage= (Tag *)[fetchedArrayObjects objectAtIndex:(NSUInteger)i];
[myTags setValue:[aMessage isSet] forKey:[aMessage tagName]];
}
return myTags;
}
Vignesh and Vin’s solutions are correct, but based on what you’ve said, the best thing to do would be to change
tagsinto astrongproperty:Unless you’re in a situation where a retain loop might arise (say, a delegate object), you should use
strongso your properties aren’t deallocated from under you.In addition, unless you’re using ARC, you’ll want to autorelease
myTags:Oh, and if you’re not using ARC, you’ll want to make sure to release
tagsindealloc.