Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

mdeh

macrumors 6502
Original poster
Jan 3, 2009
345
2
All these questions!!! :) Just when I thought things were going to get easier.

I am trying to conceptualize what happens when an object is unarchived.

So, given, Foo.m with a string Object ivar "strVal" which implements <NSCoding>

Code:
-(id) initWithCoder: (NSCoder *) decoder
{
	strVal = [ [decoder decodeObjectForKey: @"FooStrVal"]retain];
        ..............

and the documentation that
"decodeObjectForKey": Decodes and returns an autoreleased Objective-C object that was previously encoded with.........


and lastly this snippet in main.m

Code:
myFoo2 = [ NSKeyedUnarchiver unarchiveObjectWithFile: @"foo.arch"];


Again, given that myFoo2 was **not** created with any of the **magic** words which would imply ownership ( and hence warrant a release method call in main), does not the **retain** call in Foo.m need a release? Or is this a simple matter that the ivar is allocated "memory" during the instantiation of instance, ( if that even makes sense) and the retain simply matches the autorelease of the returned object by the unarchiver?
Sorry if this sounds a little confusing, but my head is spinning :) Clarity would be appreciated.
 

kainjow

Moderator emeritus
Jun 15, 2000
7,958
7
Objects created from unarchiving are autoreleased. There's nothing magical here, it follows the same pattern as any other method does. If you want to keep them around longer, retain it, which needs to be followed up with a release (usually in the dealloc method if you're assigning to an ivar).
 

mdeh

macrumors 6502
Original poster
Jan 3, 2009
345
2
(usually in the dealloc method if you're assigning to an ivar).

Kick myself!!!!!

Thank you so much...I just could not figure out where that thing was being released, so I came up with a bunch of intricate theories!!!

The joy of learning.

Kainjow...you said that
.....unarchiving are autoreleased. There's nothing magical here

Are you saying that the retain is just ordinary, or that autorelease is the way Foundation usually handles an object being returned from a collection, ( or that may be too general a statment?)
 

mdeh

macrumors 6502
Original poster
Jan 3, 2009
345
2
I'm slightly confused trying to understand where you're coming from with this.

Thanks kainjow. I guess, if , as the documentation states, the unarchived object should **not** be released by me, the programmer, it makes sense that it is "autoreleased" by it's owner, the class method.

But, the real issue for me was your insight into the release I had already written into the dealloc method.

Your help and input is much appreciated
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.