开发者

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

开发者 https://www.devze.com 2023-01-17 21:08 出处:网络
I have a class, that basically manages core data. inserts deletes and updates data. I initialize this class like so

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

- (id)init
开发者_如何学JAVA{
    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?


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];


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

0

精彩评论

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