Iphone – NSKeyedUnarchiver memory leak issue

cocoa-touchiphoneiphone-sdk-3.0

I have problem with this code, it's working on debug environment. On the instruments I'm seeing memory leak problem on this function, instruments is giving warning that

Category Event Type Timestamp Address Size Responsible Library Responsible Caller
27 SocialNetwork Malloc 00:19.951 0x3d64d20 80 Foundation -[NSKeyedUnarchiver _decodeArrayOfObjectsForKey:]

- (NSMutableArray *)GetDataInstanceToUserDefaults{
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];   
NSData *storedObject = [userDefaults objectForKey:@"MyDataKey"];
NSMutableArray *data;   

if(storedObject != nil)
{       
    NSArray *savedArray = [NSKeyedUnarchiver unarchiveObjectWithData:storedObject];
    if(savedArray != nil)
        data = [[NSMutableArray alloc] initWithArray:savedArray];
    else
        data = [[NSMutableArray alloc] init];
}else{
    data = [[NSMutableArray alloc] init];   
}   
return data; 

}

I didn't understand where is the my problem ?

Thank you for your support

Edit : By the way I should give more detail about this problem,this function (as you can see) is storing my object. My object is custom class and storing in the NSMutableArray.

I already added these methods inside of the my custom class

-(void)encodeWithCoder:(NSCoder *)coder{
-(id)copyWithZone:(NSZone*)zone {
-(id)initWithCoder:(NSCoder *)coder{

Best Answer

I think the problem is most likely in the initWithCoder: method of your custom class. It is leaking but the analyzer reports it as being in the archiver.

Unrelated to your problem, I would caution you against using [[NSMutableArray alloc] init] to initialize collections, especially mutable collections. Instead use, [[NSMutableArray alloc] initWithCapacity:1]. I've seen strange problems using just init that were cleared up by using initWithCapacity.

Related Topic