NSDictionary 作为属性
我使用以下代码收到指向“NSDictionary *dw = [NSDictionary DictionaryWithContentsOfFile:path];
”行的内存泄漏
NSDictionary *_allData;
@property (nonatomic, retain) NSDictionary *allData;
@synthesize allData = _allData;
+ (NSString*)getNSPath
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"alarm.plist"];
return path;
}
- (NSDictionary *)allData
{
NSString *path = [saveAlarm getNSPath];
NSDictionary *dw = [NSDictionary dictionaryWithContentsOfFile:path];
_allData = [NSDictionary dictionaryWithDictionary:dw];
return _allData;
}
数据在 pList 中发生变化,当我要求检索那里的新内容时通过财产然后它泄漏。 有什么建议如何说清楚吗?或者说如何实现这种不泄露的事情呢?
谢谢
I am getting memory leaks pointing to the line "NSDictionary *dw = [NSDictionary dictionaryWithContentsOfFile:path];
" by using following code
NSDictionary *_allData;
@property (nonatomic, retain) NSDictionary *allData;
@synthesize allData = _allData;
+ (NSString*)getNSPath
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"alarm.plist"];
return path;
}
- (NSDictionary *)allData
{
NSString *path = [saveAlarm getNSPath];
NSDictionary *dw = [NSDictionary dictionaryWithContentsOfFile:path];
_allData = [NSDictionary dictionaryWithDictionary:dw];
return _allData;
}
The data are changing in the pList and when I ask to retrieve what is new there by property then it leaks.
Any recommendation how to make clear? Or how to implement this kind of thing without leaks?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要在重新分配 _allData 之前释放它。分配时还需要保留它。
编辑:结合罗伯特的改进来摆脱不需要的 NSDictionary。
EDIT2:因为您要跨 API 边界返回一个对象,所以需要将其重新调整为自动释放的对象。
您发布的代码有点奇怪,因为您正在创建一个名为 allData 的属性,告诉它使用 _allData 作为 ivar (使用 @synthesize),然后实现一个设置 ivar 的自定义 getter。如果将该属性声明为只读,则可以删除 @synthesize 语句。
如果您仅在此方法中使用 _allData 而不是此类中的其他任何地方,则可以完全摆脱它。这是一个更简单的版本,可以做同样的事情:
You need to release _allData before reassigning it. You also need to retain it when you assign it.
EDIT: Incorporating Robert's improvement to get rid of an unneeded NSDictionary.
EDIT2: Because you're returning an object across an API boundary, it need to be retuned as an autoreleased object.
The code you posted is a bit odd in that you're creating a property called allData, telling it to use _allData as the ivar (with @synthesize) and then implementing a custom getter that sets the ivar. If you declare the property as readonly, you can remove the @synthesize statement.
If you're only using _allData inside this method and not anywhere else in this class, you can get rid of it entirely. Here's a much simpler version that does the same thing:
为什么不替换
为
然后你不必担心 dw NSDictionary 的自动释放,这可能会导致你的泄漏。
Why don't you replace
With
Then you don't have to worry about the dw NSDictionary's autorelease which is probably causing your leak.