开发者

Objective C: Memory Leak of Dictionary used in singleton

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


Add an autorelease:

self.dataList = [[[NSMutableDictionary alloc] init] 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

self.dataList = newValue;

@syntehsize'd properties take care of all the retain release stuff for you.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜