NSDictionary 作为属性

发布于 2024-09-26 00:01:32 字数 848 浏览 4 评论 0原文

我使用以下代码收到指向“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

愛放△進行李 2024-10-03 00:01:32

您需要在重新分配 _allData 之前释放它。分配时还需要保留它。

编辑:结合罗伯特的改进来摆脱不需要的 NSDictionary。

EDIT2:因为您要跨 API 边界返回一个对象,所以需要将其重新调整为自动释放的对象。

- (NSDictionary *)allData
{
     NSString *path = [saveAlarm getNSPath];
     [_allData release];
     _allData = [[NSDictionary dictionaryWithContentsOfFile:path] retain];

    return [_allData autorelease];
}

您发布的代码有点奇怪,因为您正在创建一个名为 allData 的属性,告诉它使用 _allData 作为 ivar (使用 @synthesize),然后实现一个设置 ivar 的自定义 getter。如果将该属性声明为只读,则可以删除 @synthesize 语句。

如果您仅在此方法中使用 _allData 而不是此类中的其他任何地方,则可以完全摆脱它。这是一个更简单的版本,可以做同样的事情:

- (NSDictionary *)allData
{
     NSString *path = [saveAlarm getNSPath];
     return [NSDictionary dictionaryWithContentsOfFile:path];
}

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.

- (NSDictionary *)allData
{
     NSString *path = [saveAlarm getNSPath];
     [_allData release];
     _allData = [[NSDictionary dictionaryWithContentsOfFile:path] retain];

    return [_allData autorelease];
}

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:

- (NSDictionary *)allData
{
     NSString *path = [saveAlarm getNSPath];
     return [NSDictionary dictionaryWithContentsOfFile:path];
}
酒浓于脸红 2024-10-03 00:01:32

为什么不替换

NSDictionary *dw = [NSDictionary dictionaryWithContentsOfFile:path];


_allData = [NSDictionary dictionaryWithDictionary:dw];

_allData = [NSDictionary dictionaryWithContentsOfFile:path];

然后你不必担心 dw NSDictionary 的自动释放,这可能会导致你的泄漏。

Why don't you replace

NSDictionary *dw = [NSDictionary dictionaryWithContentsOfFile:path];


_allData = [NSDictionary dictionaryWithDictionary:dw];

With

_allData = [NSDictionary dictionaryWithContentsOfFile:path];

Then you don't have to worry about the dw NSDictionary's autorelease which is probably causing your leak.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文