I'm having trouble figuring out how to manage memory for an instance variable that needs to maintain it's current state for a period of time, then be reassigned to newly allocated memory.
Take the following example for the instance variable "importantData".:
-(void)Update
{
importantData = [[self getObject] retain];
}
- (SomeObject *)getObject
{
SomeObject *objInstance = [[SomeObject alloc] init];
[objInstance autorelease];
return objInstance;
}
In my actual project, the getObject procedure is in a different class but I've simplified it just to get my point across. importantData must stay around be valid in between calls to Update.
Every time getObject is called, I'm allocating new memory and assigning it to importantData, correct? I figure I have to release the memory that importantData was pointing to before, right? I'm not sure how to do this properly without leaking memory or trying to reference deallocated memory. Thanks!