views:

135

answers:

2

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];
     }
}
A: 

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?)

Shaggy Frog
It's telling me a ton of objects are leaking, ranging from NSMutableArrays, CustomObject etc. They're all leaking from the same method, and as I get further down the stack trace it does say it's leaking on the initWithCoder method of CustomObject, but I've checked that out and there's no way it can be causing that -.-
Yakattak
If the mutable arrays are leaked, then whatever objects are nested inside them will also be leaked, since collections retain their objects. So I'd focus on figuring out which arrays are leaking and why first.
jlehr
But I'm not claiming ownership of any of them. I never call alloc/init/retain/copy/new etc on them. If I archive the array with alloc/init, and unarchive it, does it keep the same retain count as it did when it was archived?
Yakattak
+1  A: 

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

Macmade
They're all set to retain, and all of them are released in dealloc.
Yakattak
+1 Instruments tells you where the object was *created* that was leaked, not where you lost your last reference to it. If you forget to `release` your object in a `dealloc` method, then you'll leak an object, but it'll show you where the object was created.
Dave DeLong
I nilled/released all of them, however, it's still causing leaks.
Yakattak