Re: self = [super init];
Re: self = [super init];
- Subject: Re: self = [super init];
- From: "Clark Cox" <email@hidden>
- Date: Thu, 4 Dec 2008 13:34:25 -0800
On Thu, Dec 4, 2008 at 12:53 AM, Stephen J. Butler
<email@hidden> wrote:
> On Thu, Dec 4, 2008 at 2:32 AM, EVS <email@hidden> wrote:
>> Thank you, I suppose the reason I find it confusing is because self is a
>> pointer to the current object. If self were to release self ( the current
>> object ), while it is being used, I would have guessed that would cause
>> problems.
>
> You can call [self release] as long as you don't access (directly or
> indirectly) self for the rest of the method. So if you keep from
> touching instance variables or calling methods off self then things
> will be fine. And if your caller knows somehow (like checking return
> values, in the case of init) to not reference the released self then
> everything is kosher.
>
> In fact, the same is true in C++ with "this", only a little more
> pathologically so. You're allowed to call non-virtual member functions
> off a bad "this" pointer because
No, you're not. It is possible that many compilers will let you get
away with it, but it doesn't mean that you're "allowed" to do it.
> those functions are statically bound.
> Causes confusion in every intro C++ course because students can't
> figure out why their program is crashing in the middle of a method
> call (when they finally access an instance variable off the bad "this"
> pointer).
>
> I even saw a horrible design pattern, probably on The Daily WTF, where
> the programmer was doing singletons via something like:
>
> ((MyClass*)NULL)->getInstance();
>
> Totally pathological, but nothing technically wrong with it as long as
> you're careful.
No, according to the standard, that is undefined behavior. So not only
pathological, it is also technically wrong :).
--
Clark S. Cox III
email@hidden
_______________________________________________
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