skylerl
skylerl

Reputation: 4180

Memory leak when returning object

I have this memory leak that has been very stubborn for the past week or so. I have a class method I use in a class called "ArchiveManager" that will unarchive a specific .dat file for me, and return an array with it's contents. Here is the method:

+(NSMutableArray *)unarchiveCustomObject
{
     NSMutableArray *array = [NSMutableArray arrayWithArray:[NSKeyedUnarchiver unarchiveObjectWithFile:/* Archive Path */]];
     return array;
}

I understand I don't have ownership of it at this point, and I return it.

 CustomObject *myObject = [[ArchiveManager unarchiveCustomObject] objectAtIndex:0];

Then, later when I unarchive it in a view controller to be used (I don't even create an array of it, nor do I make a pointer to it, I just reference it to get something out of the array returned by unarchiveCustomIbject (objectAtIndex). This is where Instruments is calling a memory leak, yet I don't see how this can leak! Any ideas?

Thanks in advance.

Edit: CustomObject initWithCoder added:

-(id)initWithCoder:(NSCoder *)aDecoder
{
    if (self = [super init])
    {
        self.string1 = [aDecoder decodeObjectForKey:kString1];
        self.string2 = [aDecoder decodeObjectForKey:kString2];
        self.string3 = [aDecoder decodeObjectForKey:kString3];
        UIImage *picture = [[UIImage alloc] initWithData:[aDecoder decodeObjectForKey:kPicture]];
        self.picture = picture;
        self.array = [aDecoder decodeObjectForKey:kArray];
        [picture release];
    }
    return self;
}

Edit 2: I did a trace of CustomObject, and I realized that in my app, another object (OtherObject) has it's initWithCoder method assign a parameter of CustomObject, making it retain that CustomObject. It looks like this:

-(id)initWithCoder:(NSCoder *)aDecoder
{
     if (self = [super init])
     {
          self.customObject = [aDecoder decodeObjectForKey:kCustomObjectKey];
     }
}

Upvotes: 0

Views: 425

Answers (2)

Macmade
Macmade

Reputation: 53960

How did you declare your properties? Assign, retain, copy? Are you sure you release all of them if necessary in your dealloc method?

Upvotes: 1

Shaggy Frog
Shaggy Frog

Reputation: 27601

What exactly is Instruments telling you is leaking? Is it possible that it's your CustomObject?

(Small aside -- is there a reason why your method is returning an NSMutableArray rather than an NSArray?)

Upvotes: 0

Related Questions