Re: initWithCoder / unarchiveObjectWithData Memory Management Question
Re: initWithCoder / unarchiveObjectWithData Memory Management Question
- Subject: Re: initWithCoder / unarchiveObjectWithData Memory Management Question
- From: Quincey Morris <email@hidden>
- Date: Sat, 20 Feb 2010 00:21:40 -0800
On Feb 19, 2010, at 22:47, Thomas Wetmore wrote:
> from the NSKeyedUnarchiver (or any unarchiver) class.
>
> After the following lines of code run ...
>
> NSData* data = [NSData dataWithContentsOfFile: myObjectPath];
> myObject = [NSKeyedUnarchiver unarchiveObjectWithData: data];
>
> (error handling removed), has myObject been retained, or must I now retain it.
It's a simple application of the memory management rules. The method name doesn't meet the criteria of methods that return an object you own, so you don't own it. So you must take ownership, by retaining it (for example).
> The unarchiveObjectWithData class method causes myObject's class's initWithCoder object method to run.
This is irrelevant to the ownership of the value returned by 'unarchiveObjectWithData'.
> I assume that initWithCoder obeys memory management rules and returns a newly alloc'ed and retained object.
Nope, not even close. An object is allocated *before* 'init...' is called, and the 'init...' method has *no* role in determining the ownership of the object being initialized.
> Does the unarchiveObjectWithData method return that retained object unaltered, or does it autorelease it first, so that the unarchiveObjectWithData method obeys the usual convention of class factory methods to return autoreleased objects?
You don't care. Whether the method temporarily takes ownership of the new object is an implementation detail. All you care about is whether it returns an object you own.
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden