Objective C:单例中使用的字典内存泄漏
我正在使用单例类在我的 iPhone 应用程序中的视图之间共享数据。我的单例类包含一个我在 -init 方法中分配的字典:
- (id)init
{
if ( self = [super init] )
{
self.dataList = [[NSMutableDictionary alloc]init];
}
return self;
}
我在 dealloc 方法中释放它:
- (void)dealloc
{
[dataList release];
[super dealloc];
}
这个 dataList 是从服务器下载的,我在我的应用程序中多次执行此操作,因此我有一个自定义的 setter 方法来释放旧的,并保留新的:
-(void) setDataList:(NSMutableDictionary*)d
{
if( dataList !=nil){
[dataList release];
dataList = [d retain];
else
dataList = [d retain];
}
在使用泄漏工具时,我发现字典出现内存泄漏。我认为我正在正确地分配和释放字典。是否因为单例的 dealloc 方法没有被调用而发生泄漏?
感谢您的帮助,
斯里坎特
I am using a singleton class to share data between views in my iphone app. My singleton class contains a dictionary which I allocate in my -init method:
- (id)init
{
if ( self = [super init] )
{
self.dataList = [[NSMutableDictionary alloc]init];
}
return self;
}
I release it in my dealloc method:
- (void)dealloc
{
[dataList release];
[super dealloc];
}
This dataList is downloaded from a server, and I do this multiple times in my app,so I have a custom setter method to release the old one, and retain the new one:
-(void) setDataList:(NSMutableDictionary*)d
{
if( dataList !=nil){
[dataList release];
dataList = [d retain];
else
dataList = [d retain];
}
ON using the leaks tool, I am getting a memory leak of the dictionary. I think I am doing the alloc and release of the dictionary properly..does the leak occur because the dealloc method of the singleton is not getting called?
Thanks for your help,
Srikanth
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
添加自动释放:
当你将一个对象分配给一个属性时,它会保留它,并且每当你调用 init 方法时,它都会保留它,从而使保留计数达到 2。
当你重新分配它时,它也会释放它,这样你就可以只
使用 @syntehsize'd 属性为您照顾所有保留释放的东西。
Add an autorelease:
When you assign a an object to a property it retains it and whenever you call and init method it retains, bringing the retain count to 2.
It also releases when you reassign it so you can just
@syntehsize'd properties take care of all the retain release stuff for you.