+(NSMutableDictionary*)getList:(NSString *)key
{
......
NSMutableDictionary *dict=[[NSMutableDictionary alloc] initWithContentsOfFile:key];
return dict;
}
调用时
NSMutableDictionary *obj = [self getList:key];
[obj.release];
Instruments中偶尔提示内存泄漏 指向
NSMutableDictionary *dict=[[NSMutableDictionary alloc] initWithContentsOfFile:key];
请教问题出在哪里?
------解决方案--------------------
- C/C++ code
+(NSMutableDictionary*)getList:(NSString *)key{ //使用autorelease [NSMutableDictionary *dict=[[NSMutableDictionary alloc] initWithContentsOfFile:key] autorelease]; return dict;}
------解决方案--------------------
1,NSMutableDictionary *obj = [self getList:key];
[obj.release];
是不对的,肯定报错的
因为(getList:)是+
2,代码在释放池中的话,可以返回 autorelease
或者调用后自动释放
NSMutableDictionary *obj = [类 getList:key];
这里如果是在普通函数中的话,大可不用管。因为函数结束后,所有的引用地址会自动释放。 *obj是指针,没有alloc。
------解决方案--------------------
obj 设置为类中的成员变量,
obj=[[NSMutableDictionary alloc] init];
obj=[self getlist:key];
试试看。
------解决方案--------------------
最佳写法:
+(NSMutableDictionary*)getList:(NSString *)key
{
......
NSMutableDictionary *dict=[[NSMutableDictionary alloc] initWithContentsOfFile:key];
return [dict autorelease];
}
记得外部不需要release了。