On Fri, Oct 3, 2008 at 4:29 PM,  <[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];

The line for S3 is wrong since you presumably want S3 to remain valid
for the life of your object (or until otherwise set). +[NSString
string] is returning you an object that you don't "own" so it will
disappear out from under you at some point in the future unless you
either take ownership directly (retain it) or indirectly (for example
add it to a collection your object owns). Often best to always
directly retain the objects you reference via instance vars unless the
ivar is meant to be a weak reference.

> 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?

You should release S1, S2, and S3 in your dealloc method (assuming you
correct what you are doing with S3).

If no other code changes S1 you could skip releasing S1 but since
releasing a string constant is a no-op you should continue to release
S1 in your dealloc method for code readability and correctness. This
can help prevent a leak in the future if you decide to make S1 point
at a string you get / create from some other code but forget to add
the now needed release.

-Shawn
_______________________________________________

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