On 4 Oct 2008, at 9:29 am, [EMAIL PROTECTED] wrote:

But now I'm confused about how to de-allocate MyClass. Given this in its initialisation:

        S1 = @"a string";
        S2 = [[NSString alloc] init];
        S3 = [NSString string];

I would only release S2 in the dealloc method. But if the class has been unarchived, won't I leak memory with S1 and S3 when the class is released?


An approach that might help clarify this is to have accessor methods to set each string, rather than assign them directly in the initWithCoder method. Then you can write something like:

[self setFirstString:[coder decodeObjectForKey:@"blah"]];

then there's no special case when dearchiving compared with setting the string from any other place. And of course, the accesssor method would, as is the norm, retain the object.


Sending -retain to a constant literal string is fine, so you don't need to treat those as a special case either. So again:

[self setFirstString:@"Initial Value"];

is fine, even if the -setFirstString method retains the string, as it should.


hth,

Graham
_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [EMAIL PROTECTED]

Reply via email to