retain/release rules
retain/release rules
- Subject: retain/release rules
- From: Peter Jergens <email@hidden>
- Date: Tue, 30 Jul 2002 17:53:12 -0400
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.