Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

retain/release rules



I'm having a problem with objects disappearing from memory, and I'm assuming it means I have an insufficient understanding of the correct use of retain, release, autorelease, &c.

The problem appears when I try to load an object using NSUnarchiver (having already stored it with NSArchiver). The data initially appears to be there, but when I try to do anything to the object it appears to have already vaporized. (specifically, accessing another element of the array is where I'm failing right now - I appear to be holding on to the "current" one long enough to change it)

The data structure looks something like this: the data object has a NSString name and a NSMutableArray of instances of another object, which has a pair of NSString in it. The data object also keeps a pointer to one of the objects in the array as the object currently being edited.

My accessors generally look something like this:
-(void)setGroupName:(NSString*)in_value {
[_groupName autorelease];
_groupName = [in_value copy];
}

and my decoders like this:
-(id)initWithCoder:(NSCoder*)coder {
int i;
_groupName = [[coder decodeObject] retain];
_element = [[NSMutableArray arrayWithArray:[coder decodeObject]] retain];
return self;
}

and my initializers like this:
-(id)init {
_groupName = @"noname";
_currentElement = [[hpart alloc] init];
_element = [[NSMutableArray array] retain];
[_element addObject:_currentElement];
return self;
}

My general understanding is that I'm responsible for releasing objects I create with lines like "_currentElement = [[hpart alloc] init]" or "_memberName = [in_value copy]" but not anything else; if I want to keep another object I have to retain it.

(BTW, the "_currentElement" is supposed to point to an object contained in the "_element" array for easier access - it is not a separate element)

Am I missing something obvious?
-Peter
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.