• John Doe (unregistered) in reply to Mogri
    Mogri:
    This comment wins. (Or should that be "this->self comment wins"?)

    Ooh, new WTF meme!

    Captcha (sorry, don't do this normally, but it's fitting here): pointer

  • John Doe (unregistered) in reply to IV
    IV:
    2. When self is deinitialized, doesn't that deinitialize this as well? So isn't that memory freed, allowing the return that is implied at the end of the deinitialized function to be overwritten? If I am right, this is a potential for a very hard to find bug. If I am wrong, someone will correct me, and I will learn something else new today.
    Nah, I don't think fnCERTlibDeinitialize frees the object. This would be especially bad, since this function is called from within one of the class methods. The fact that "this->self" is passed is not different from passing "this". But who knows, with code like this all bets are off...
  • (cs) in reply to IV
    IV:
    When self is deinitialized, doesn't that deinitialize this as well?
    No. When self is uninitialized, this is uninitialized as well, and that is not defined.
    IV:
    So isn't that memory freed, allowing the return that is implied at the end of the deinitialized function to be overwritten?
    Kinda, but that was never allocated.
    IV:
    If I am right, this is a potential for a very hard to find bug. If I am wrong, someone will correct me, and I will learn something else new today.
    Except this is a CERTlib not a potential.
    -dZ.
    
  • Franz Kafka (unregistered)

    Looks like someone is clobbering their this pointer and added a self pointer rather than fix the corruption. Tread carefully.

  • AC (unregistered) in reply to John Doe
    John Doe:
    IV:
    2. When self is deinitialized, doesn't that deinitialize this as well? So isn't that memory freed, allowing the return that is implied at the end of the deinitialized function to be overwritten? If I am right, this is a potential for a very hard to find bug. If I am wrong, someone will correct me, and I will learn something else new today.
    Nah, I don't think fnCERTlibDeinitialize frees the object. This would be especially bad, since this function is called from within one of the class methods. The fact that "this->self" is passed is not different from passing "this". But who knows, with code like this all bets are off...

    One possibility is that fnCERTlibDeinitialize takes a CERTLib*& rather than a CERTLib* and nulls out the parameter, though this could still be handled via a local rather than a field.

    The Delphi "compat" idea still sounds more plausible to me.

  • (cs) in reply to Synonymous Coward
    Synonymous Coward:
    John Doe:
    It seems this class has some schizofrenic potential. It will become truly schizofrenic if self is pointed to a new class instance. To make it interesting, this should be decided at random in the constructor, with a small change for a split personality.

    The default copy constructor would give you something similar for free:

    Oh, ouch. Good catch.

  • (cs) in reply to Franz Kafka
    Franz Kafka:
    Looks like someone is clobbering their this pointer and added a self pointer rather than fix the corruption.
    Selfish, isn't it.
  • Edward Royce (unregistered)

    Hmmmm.

    Now that's bootstrapping!

  • (cs) in reply to Anon
    Anon:
    Anonymous Coward:
    Please, *please* can someone disable those annoying "Frist" comments?
    I propose a simple solution: if there aren't any posts yet, the first one gets deleted. ¬_¬

    If there aren't any posts yet, there's nothing to delete. 8=]

    dzimm:
    Delphi uses "Self". I program in both Delphi and JAVA and I struggle with using the wrong identify keyword. I can imagine someone got sick of trying to remember which one to use and he made it so it didn't matter

    This CodeSOD also needs a 'Me' member. ('MyClass' apparently cannot be implemented in C++?)

  • David (unregistered) in reply to Anonymous Coward
    Anonymous Coward:
    Please, *please* can someone disable those annoying "Frist" comments?

    Well, you could disable posting until one post is posted, but there wouldn't be much comments then é? :)

  • sammy (unregistered)

    Well, obviously you need that. I mean, what if you go to deinitialize the object, and the object is deinitialized before you have the chance to deinitialize it? How would you even refer to it?

    Obviously, you'd use self.

    I think.

    Right?

  • (cs)

    This line

    var self = this;

    is quite common in Javascript. It's useful when working with closures. For example:

    var self = this;
    this.someEvent = function()
    {
      // Could be true or false depending
      // on when or how the function is called
      alert(this === self);
    };
    

    Threw me through a loop the first time I encountered it.

  • Csaboka (unregistered) in reply to Spectre
    Anonymous Coward:
    Please, *please* can someone disable those annoying "Frist" comments?

    I've seen a more creative way to fight "first" posts on some blog: the original poster adds a "first" comment himself right after posting the article. Trolls seem to go away when they see there is a "first" already there :)

  • Cope with IT (unregistered) in reply to rawsteak
    rawsteak:
    this->self and you are this! but if self = this, then who are you? I am this! I am this and you are self! but if you are this and I am self, and this->self and self = this, then who is he??
    ...and to cite a little bit closer to the original:

    I am he as you are he as you are me and we are all together. ... I am the walrus...

  • Jon (unregistered)

    delete this;

  • Yokel (unregistered)

    I have seen this used before in conjunction with a Singleton pattern that was then inherited. So this way you have generic Actor pointers that may really be some super subclass of Actor.

    That code was some fun stuff.

  • (cs) in reply to John Doe
    John Doe:
    It seems this class has some schizofrenic potential. It will become truly schizofrenic if self is pointed to a new class instance. To make it interesting, this should be decided at random in the constructor, with a small change for a split personality.

    Here is what I mean:

    CERTlib::CERTlib(...) 
    {
       // Store ptr to self
       if(rand() % 100 < 3)
          self = new CERTlib();
       else
          self = this;
       ...
    }

    Strictly speaking, that wouldn't be schizophrenic; that would be a schizoid personality. Schizophrenia is a disease, or syndrome, or what have you, that was mis-named in around 1890, and even the originator of the name got annoyed by its abuse.

    Speaking as someone who has had to bail out a true paranoid schizophrenic, I should know.

    I'm sure somebody has mentioned this by now, but my guess is that the coder of this truly awesome snippet started life in Perl and moved on to C++ whilst leaving their brain cell behind. SysAdmins, doncha just love them?

    (I'll go away and read the rest of the posts now. Sorry.)

  • Andrew (unregistered) in reply to Dan
    Dan:
    dzimm:
    Delphi uses "Self". I program in both Delphi and JAVA and I struggle with using the wrong identify keyword. I can imagine someone got sick of trying to remember which one to use and he made it so it didn't matter

    So instead of spending the 10 seconds to get it right, program something in that will completely baffle the next programmer that actually knows what they're doing? Brilliant...

    This won't just confuse programmers. It'll confuse programs! The object has a circular reference to itself.

    Try to clone the object A using a deep copy. Traverse object fields from A to A.self, which is also A! Keep going...

    I'd love to see whether this will break a Garbage Collector. I think early early Java interpreters had similar problems with link lists.

  • (cs) in reply to C++ Guru
    C++ Guru:
    tag:
    Guessing the cause: If the destructor is called before deinitialize, this pointer may get unavailable. Maybe self is still available? It is so ugly ... marvellous! ;-)

    In C++, any access to an object after the destructor is called is undefined. This means the program is free to do nothing, throw an exception, or reformat your hard drive. All would be valid consequences.

    "self" is an instance member of the class, and is therefore unavailable after the destructor is called. Moreover, any copies of "self" squirreled away somewhere else are now dangling pointers to a destructed object. They are just undefined behavior waiting to happen.

    C++ is hard.

    I don't think that stating the bleeding obvious makes you a C++ Guru. And, although we both know that "undefined behaviour" is generally demonised as "is free to wipe out your hard drive," I assume that we both know that this is a Brothers Grimm story from Bjarne. It just ain't gonna happen.

    C++ is not hard. Programmers are just soft.

    And I hate to do this, but I've an itch at the base of my medulla:

    zolf:
    self->self->self->self->self->self->self->self->self->self->self->self->self->self->self->self->self->WTF()

    (Using generally recognised coding standards for the member function.)

    Isn't code review a beautiful thing?

    Pass the spam ...

  • Andrew (unregistered) in reply to Yokel
    Yokel:
    I have seen this used before in conjunction with a Singleton pattern that was then inherited. So this way you have generic Actor pointers that may really be some super subclass of Actor.

    That code was some fun stuff.

    Wouldn't it be easier to enforce the Singleton pattern in the constructor? Use a static boolean flag, and throw an exception when a second object is declared.

  • Mexi-Fry (unregistered)

    class CERTlib { private: CERTlib* what; // Pointer to this CERTlib* kind; // Pointer to this CERTlib* self; // Pointer to this CERTlib* shit; // Pointer to this CERTlib* _this; // Pointer to this CERTlib* like; // Pointer to this CERTlib* retard; // Pointer to this CERTlib* _of; // Pointer to this CERTlib* write; // Pointer to this CERTlib* would; // Pointer to this

    public CERTlib* Result() { return what->kind->_of->retard->would->write->shit->like->_this; // ? }

    /// CAPTCHA : digdug : and still didn't find anything. }

  • (cs) in reply to Andrew
    Andrew:
    Yokel:
    I have seen this used before in conjunction with a Singleton pattern that was then inherited. So this way you have generic Actor pointers that may really be some super subclass of Actor.

    That code was some fun stuff.

    Wouldn't it be easier to enforce the Singleton pattern in the constructor? Use a static boolean flag, and throw an exception when a second object is declared.

    Or, in fact, not use a Singleton class at all? These things are pure evil. If you really have to, use a namespace and static functions, for God's sake.
  • (cs) in reply to John Doe
    John Doe:
    It seems this class has some schizofrenic potential. It will become truly schizofrenic if self is pointed to a new class instance. To make it interesting, this should be decided at random in the constructor, with a small change for a split personality.

    Here is what I mean:

    CERTlib::CERTlib(...) 
    {
       // Store ptr to self
       if(rand() % 100 < 3)
          self = new CERTlib();
       else
          self = this;
       ...
    }

    How about if self is pointing to an old instance? Suppose the class is lacking a proper copy ctor. (Not too hard to imagine.) The self pointer would likely end up pointing to the source of the copy.

    Maybe this is the programmer's equivalent to that song "I'm My Own Grandpa".

  • Graham_C (unregistered) in reply to dzimm
    dzimm:
    Delphi uses "Self"

    So does Objective-C for that matter.

  • (cs)

    You know, this actually isn't such a WTF. By issuing a reference to itself, inner non-static classes can access the outer class or in java anonymous (handlers) class. Such as when you need to issue a dialog from a action listener on a button. How do you get a reference to the main dialog?

    You can initialize a self reference as a class member variable.

    JFrame self = this;

    Then in the anonymous action listener you can use that in JOptionPane.showMessageDialog to display the dialog for the correct parent.

    Perhaps something similar(a C++ version) occurred and the code was removed later which is why there is no other evidence?

    Addendum (2007-09-13 00:43): Huh, been informed by a co-worker that MyOuterClass.this works too. Well, you learn something new everyday.

  • iMalc (unregistered)

    Forgetting whether the language uses 'this' or 'self' still isn't an excuse in this case because they still dereferenced 'this' to get to self anyway.

  • dkf (unregistered) in reply to iMalc
    iMalc:
    Forgetting whether the language uses 'this' or 'self' still isn't an excuse in this case because they still dereferenced 'this' to get to self anyway.
    That's right. It's the 'this->self' that hurts. That's Just Plain Wrong. (If they've got 'this', they can refer to 'self' directly. But if they've got 'this' they don't need 'self' anyway. And it's all private anyway, so funky shit isn't happening and the whole lot could be thrown out.) It's when you see things like this that you start to think in terms of refactoring the code with the help of 'rm' (or better yet, a large magnet on the hard drive...)
  • 28% genius (unregistered)

    Would this(self) confuse the garbage collector? Reference count never goes to zero so the object never gets garbage collected.

  • SlyEcho (unregistered) in reply to Spectre
    Spectre:
    Anon:
    I propose a simple solution: if there aren't any posts yet, the first one gets deleted. ¬_¬

    If there aren't any posts yet, there's nothing to delete. 8=]

    What about: delete first post when number of posts = 1?

  • Csaboka (unregistered) in reply to 28% genius

    Any half-decent garbage collector should handle circular references without problems. A GC doesn't keep reference counts, but discovers reachable objects instead. If you can't reach something from globals, or locals currently on the stack, it can be reaped, no matter how complicated links it has to other objects (or itself, for that matter).

  • danio (unregistered) in reply to dzimm
    dzimm:
    Delphi uses "Self". I program in both Delphi and JAVA and I struggle with using the wrong identify keyword. I can imagine someone got sick of trying to remember which one to use and he made it so it didn't matter

    Hopefuly they also remembered to do #define begin { #define end } #define == eq just to make things easier

  • (cs) in reply to JamesKilton
    class MyObject {
      private MyObject me;
      private MyObject myself;
      private MyObject andI;
    
      public MyObject() {
        this.andI   = this;
        this.myself = this.andI;
        this.me     = this.myself.andI;
      }
    
      public void xxx() {
        me.myself.andI.whatever();
      }
    }
    
  • (cs) in reply to snoofle
    snoofle:
    class MyObject {
      <snip>
    }
    
    Shouldn't that be "class MeObject"?
  • The Anonymous Coward (unregistered) in reply to Steve H.
    Steve H.:
    My thought was on this was that the original programmer either was taught to do this or instinctively does this for static methods, where you don't get *this

    Ah, yes, very clever. Except that since self isn't defined as static, non-static methods won't be able to access it either.

    The only thing I can think of (if the original WTF was indeed in C++, as depicted in the submission), is this might have something to do with virtual inheritance. Either he isn't using it but should be... or maybe is using it and needed to work around some side-effect... been too long since I've thought about virtual inheritance to be sure.

    And BTW, last I knew (I've been out of the C++ loop for a while now), there was no GC in C++; so I doubt it's anything to do with that.

  • The Anonymous Coward (unregistered) in reply to Steve H.
    Steve H.:
    My thought was on this was that the original programmer either was taught to do this or instinctively does this for static methods, where you don't get *this

    Ah, yes, very clever. Except that since self isn't defined as static, non-static methods won't be able to access it either.

    The only thing I can think of (if the original WTF was indeed in C++, as depicted in the submission), is this might have something to do with virtual inheritance. Either he isn't using it but should be... or maybe is using it and needed to work around some side-effect... been too long since I've thought about virtual inheritance to be sure.

    And BTW, last I knew (I've been out of the C++ loop for a while now), there was no GC in C++; so I doubt it's anything to do with that.

  • pooh (unregistered)

    Potential solution: What if the deinitializer is something like this:

    fnCRTLibDeinitialize(CRTLib &*pointerref) { blah blah; pointerref = NULL; }

    Seen code like that - perhaps that's the case.

  • C++ Shishya (unregistered) in reply to C++ Guru
    C++ Guru:
    C++ is hard.

    C++ is fun.

  • free(this->self) (unregistered) in reply to snoofle

    Two problems I keep seeing in these comments:

    1. If "it's not a WTF because it's valid code because of this and this", it's a WTF because they did something so strange without documenting why.

    2. If they really couldn't remember whether to use self or this: #define self this

    snoofle:
    class MyObject {
      private MyObject me;
      private MyObject myself;
      private MyObject andI;
    

    public MyObject() { this.andI = this; this.myself = this.andI; this.me = this.myself.andI; }

    public void xxx() { me.myself.andI.whatever(); } }

    Don't you mean: (me.myself & i).whatever();?

  • The_Assimilator (unregistered)

    I'm 100% sure this was a Delphi programmer trying to write C++... our company is currently migrating from Delphi to C#, and one of the more "experienced" programmers decided to use this exact same trick to "help with the transition".

    Thankfully I caught this behavior before everyone else started copying him; needless to say, he uses "this" nowadays.

    On an entirely different topic, Borland should burn in hell for creating the monstrosity that is Object Pascal. It makes me sick just reading it.

  • AdT (unregistered) in reply to Mike Nuss
    Mike Nuss:
    Java is not an acronym.

    Just Avoid Vain Annotations.

  • The Jerk (unregistered) in reply to The_Assimilator
    I'm 100% sure this was a Delphi programmer trying to write C++... our company is currently migrating from Delphi to C#, and one of the more "experienced" programmers decided to use this exact same trick to "help with the transition".

    Thankfully I caught this behavior before everyone else started copying him; needless to say, he uses "this" nowadays.

    On an entirely different topic, Borland should burn in hell for creating the monstrosity that is Object Pascal. It makes me sick just reading it.

    Remember, the guy who designed Delphi (Object Pascal) is the same guy Microsoft hired to design C#.

  • (cs)

    I actually had to do this once, except it wasn't a bare pointer. It went something like this:

    class BaseObject
    {
        WeakPointer<BaseObject> This;
        void InitializeThis(WeakPointer<BaseObject> This)
        {
            this.This = This;
        }
        SmartPointer<ChildClass> CreateChild()
        {
            SmartPointer<ChildClass> MyChild = new ChildClass();
            MyChild.Initialize(MyChild);
            MyChild.SetParent(This);
            return MyChild;
        }
    };
    

    Of course, the reason for this was the memory management. One couldn't pass the this pointer because that would initialize a new SmartPointer class resulting in multiple deletes. So, at least it makes sense, I suppose. Unlike the OP.

  • Anonymous Coward (unregistered) in reply to Corporate Cog

    I don't know if this was posted but ummm #define self this ??

  • bull (unregistered) in reply to Crash magnet

    If he is trying to use self instead of this because he moved to a new language then it's excusable . The real wtf is this->self, what kind of moron is this?

    Crash magnet:
    Bob:
    That's hilarious!!! Look what I can do:

    fnCERTlibDeinitialize( this->self->self ); fnCERTlibDeinitialize( this->self->self->self ); fnCERTlibDeinitialize( this->self->self->self->self ); ... etc.

    Consider this:

    this->self->self->fnCERTlibDeinitialize( this->self->self ); this->self->self->self->fnCERTlibDeinitialize( this->self->self->self ); this->self->self->self->self->fnCERTlibDeinitialize( this->self->self->self->self );

    Crash Magnet

    Nice suicidal LOL . You don't want to kill yourself but you ask yourself to kill the self of yourself which is yourself's self. NO, it's actually yourself's self's self that kill you or the self of yourself by the command of yourself but not you...

    Stop here, this self-reflecting thing is getting me crazy.

    captcha: Yummy

  • clover (unregistered) in reply to JamesKilton
    JamesKilton:
    I LOL'd on this one. Pure awesomeness.
    i love that one it is sooooooooo funny .were didyou get it ?did you get it off of a class ego
  • clover (unregistered) in reply to JamesKilton
    JamesKilton:
    I LOL'd on this one. Pure awesomeness.
    i love that one it is sooooooooo funny .were didyou get it ?did you get it off of a class ego
  • (cs)

    I learnd programming in Oberon, a language which does not support constructors. So a way to work around the issue is to write an 'init' method which functions as a constructor. I used to create a pointer-member which pointed to the object, exactly like this example. This pointer was set in the init-method. Another method which every class had was something like: boolean isInit(){ return (this.self == this);}. Every method begun with: assert isInit()

    Then I learnd c++ and loved it's constructors!

  • ELIZA (unregistered) in reply to The Jerk
    The Jerk:
    I'm 100% sure this was a Delphi programmer trying to write C++... our company is currently migrating from Delphi to C#, and one of the more "experienced" programmers decided to use this exact same trick to "help with the transition".

    Thankfully I caught this behavior before everyone else started copying him; needless to say, he uses "this" nowadays.

    On an entirely different topic, Borland should burn in hell for creating the monstrosity that is Object Pascal. It makes me sick just reading it.

    Remember, the guy who designed Delphi (Object Pascal) is the same guy Microsoft hired to design C#.

    ["religion"]

    2 Microsoft 3:14:
    And the Xenith were concerned by the commotion in the development room that they wondered why it was so, and that of them, che who was known as Tristique the gatekeeper against the machineservants, gazed upon the abomination that was Object Pascal, and became filled with outrage.
    15:
    And it came to pass that che was unable to subject cher kin to the foulness of the language but did not wish to punish him, and che searched for inspiration in the fourth book of giving one's life expensively, and was inspired that if not cher, then who else.
    16:
    And che therefore cursed the darkened soul whose foul mind spawned the evil that he could never forget the consequences of the atrocity he authored, and even unto this day far beyond the days of .NET, he still writes the materials of which others weave the fabric of programs, and he is chastened and haunted still by the ghosts of the Delphic terror, and it will come to pass that he will not leave this vale of woe before the completion of his penance, which the Adjudicator Seraphim whom the Ancients called Deborah, who is the entgegen kin of the degree of the sibling to Tristique, hath decreed to be the emancipation of the children of the tongue of BASIC from their bondage at the hands of the children of the unholy script known as Shub-Internet.
    You do not want to know the terrible fate assigned to the creators of COBOL, but I believe it is in 2 Fortran. [/"religion"]

Leave a comment on “Class Ego”

Log In or post as a guest

Replying to comment #:

« Return to Article