【发布时间】:2012-03-27 16:06:43
【问题描述】:
在我的 TaggingScreen.m 初始化函数中,我这样做了 -
tags = [myTagMgr getMyTags];
在我的 getMyTags 方法中,我执行以下操作 -
NSMutableDictionary *myTags = [NSMutableDictionary new];
....
return myTags;
我在这个方法中得到了 myTags 的内存泄漏。我应该在哪里释放内存? “tags”是一个在整个 TaggingScreen 类中使用的属性。因此,如果我执行自动释放,当我尝试访问类的其他方法中的标签时,会收到一个异常消息“发送到已释放实例的消息”。
编辑:
- (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;
}
【问题讨论】:
-
@property是什么tags?? -
它是一个@property (nonatomic, assign) NSMutableDictionary *tags;
标签: ios memory-leaks nsmutabledictionary