Rupert
Rupert

Reputation: 1

Iphone: Is this a bad Idea? Memory Management/Leak Issue

I have a class, that basically manages core data. inserts deletes and updates data. I initialize this class like so

- (id)init
{
    self = [super init];
    if (self) {
        self.itemList = [NSDictionary dictionaryWithObjectsAndKeys: // <== LEAKS
                    ITEMURL1, KEY1,
                    ITEMURL2, KEY2,
                    ITEMURL3, KEY3,
                    ITEMURL4, KEY4,
                    ITEMURL5, KEY5,
                    nil];
        self.storedItems = [[NSMutableArray alloc] init]; // <== LEAKS

    }
    return self;
}


- (void)dealloc {
    [storedItems release];
    [super dealloc];
}

I have taken care that I release every object carefully. whenever it is allocated, But I still seem to get leaks at init when I run the app in Instruments.

what is going on? am I doing something wrong?

Upvotes: 0

Views: 116

Answers (2)

Andrew Madsen
Andrew Madsen

Reputation: 21373

You need to add [itemList release] to your dealloc method (assuming you've got "retain" in the property declaration.

As for storedItems, if you've got retain in the property declaration, you should autorelease it when you assign it:

self.storedItems = [[[NSMutableArray alloc] init] autorelease];

Or more concisely, but equivalently:

self.storedItems = [NSMutableArray array];

Upvotes: 1

nacho4d
nacho4d

Reputation: 45118

Regarding storedItems:

if storedItems is declared as

@property (nonatomic, retain) NSMutableArray *storedItems;

(the retain part is the important) then you need to do:

NSMutableArray *storedItems_ = [[NSMutableArray alloc] init];
self.storedItems = storedItems_;
[storesItems_ release];

alloc + init will cause your object9¥'s retain count go to 1. Then using self.xxx will retain it again as declared in the attributes of your property. Hence it will be 2. This will not happen with assign

Upvotes: 0

Related Questions