Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Other Discussions
  3. Clever Code
  4. Missing something?

Missing something?

Scheduled Pinned Locked Moved Clever Code
comtoolsperformancehelpquestion
11 Posts 7 Posters 3 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Shog9 0

    Here's another "oh, duh" bug, in the same vein as Rob's:

    class IWorkItem
    {
       public:
          virtual bool DoWork()=0;
    };

    class CSpecificTask : public IWorkItem
    {
       public:
          CSpecificTask(LPCTSTR szName) : m_strName(szName) {}
          ~CSpecificTask()
          {
             //...
          }
         
          virtual bool DoWork()
          {
             //...
          }
         
       private:
          CString m_strName;
    }

    //...

    void AddNewWorkItem()
    {
       EnqueueWorkItem(new CSpecificTask(_T("Blah")));
    }

    void ProcessWorkItem()
    {
       IWorkItem* pItem = PopWorkItem();
       // ...
       delete pItem;
    }

    Musta stared at memory leak reports for a good two hours last night, before giving up and going to take a shower. As i was washing my face, i realized what i'd missed... :doh: :-O

    every night, i kneel at the foot of my bed and thank the Great Overseeing Politicians for protecting my freedoms by reducing their number, as if they were deer in a state park. -- Chris Losinger, Online Poker Players?

    D Offline
    D Offline
    dighn
    wrote on last edited by
    #2

    virtual dtor?

    S 1 Reply Last reply
    0
    • S Shog9 0

      Here's another "oh, duh" bug, in the same vein as Rob's:

      class IWorkItem
      {
         public:
            virtual bool DoWork()=0;
      };

      class CSpecificTask : public IWorkItem
      {
         public:
            CSpecificTask(LPCTSTR szName) : m_strName(szName) {}
            ~CSpecificTask()
            {
               //...
            }
           
            virtual bool DoWork()
            {
               //...
            }
           
         private:
            CString m_strName;
      }

      //...

      void AddNewWorkItem()
      {
         EnqueueWorkItem(new CSpecificTask(_T("Blah")));
      }

      void ProcessWorkItem()
      {
         IWorkItem* pItem = PopWorkItem();
         // ...
         delete pItem;
      }

      Musta stared at memory leak reports for a good two hours last night, before giving up and going to take a shower. As i was washing my face, i realized what i'd missed... :doh: :-O

      every night, i kneel at the foot of my bed and thank the Great Overseeing Politicians for protecting my freedoms by reducing their number, as if they were deer in a state park. -- Chris Losinger, Online Poker Players?

      K Offline
      K Offline
      Kevin McFarlane
      wrote on last edited by
      #3

      I ain't done any C++ for a couple of years. Is it lack of virtual destructor?

      Kevin

      S 1 Reply Last reply
      0
      • S Shog9 0

        Here's another "oh, duh" bug, in the same vein as Rob's:

        class IWorkItem
        {
           public:
              virtual bool DoWork()=0;
        };

        class CSpecificTask : public IWorkItem
        {
           public:
              CSpecificTask(LPCTSTR szName) : m_strName(szName) {}
              ~CSpecificTask()
              {
                 //...
              }
             
              virtual bool DoWork()
              {
                 //...
              }
             
           private:
              CString m_strName;
        }

        //...

        void AddNewWorkItem()
        {
           EnqueueWorkItem(new CSpecificTask(_T("Blah")));
        }

        void ProcessWorkItem()
        {
           IWorkItem* pItem = PopWorkItem();
           // ...
           delete pItem;
        }

        Musta stared at memory leak reports for a good two hours last night, before giving up and going to take a shower. As i was washing my face, i realized what i'd missed... :doh: :-O

        every night, i kneel at the foot of my bed and thank the Great Overseeing Politicians for protecting my freedoms by reducing their number, as if they were deer in a state park. -- Chris Losinger, Online Poker Players?

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #4

        I got myself a copy of PC Lint (and VisualLint) last week and this would of been picked up - lack of a virtual destructor in the base class is such a common mistake, I'm surprised compiler vendors don't explicitly check for it.


        Kicking squealing Gucci little piggy.

        S N 2 Replies Last reply
        0
        • K Kevin McFarlane

          I ain't done any C++ for a couple of years. Is it lack of virtual destructor?

          Kevin

          S Offline
          S Offline
          Shog9 0
          wrote on last edited by
          #5

          Yup.

          every night, i kneel at the foot of my bed and thank the Great Overseeing Politicians for protecting my freedoms by reducing their number, as if they were deer in a state park. -- Chris Losinger, Online Poker Players?

          K H 2 Replies Last reply
          0
          • D dighn

            virtual dtor?

            S Offline
            S Offline
            Shog9 0
            wrote on last edited by
            #6

            That's the one. :)

            every night, i kneel at the foot of my bed and thank the Great Overseeing Politicians for protecting my freedoms by reducing their number, as if they were deer in a state park. -- Chris Losinger, Online Poker Players?

            1 Reply Last reply
            0
            • L Lost User

              I got myself a copy of PC Lint (and VisualLint) last week and this would of been picked up - lack of a virtual destructor in the base class is such a common mistake, I'm surprised compiler vendors don't explicitly check for it.


              Kicking squealing Gucci little piggy.

              S Offline
              S Offline
              Shog9 0
              wrote on last edited by
              #7

              Rob Caldecott wrote:

              lack of a virtual destructor in the base class is such a common mistake, I'm surprised compiler vendors don't explicitly check for it.

              Well, it'd get pretty annoying in some places. I have a few sets of classes that are always instantiated and destroyed by various owners, but used by other code that requires only that they implement a pure virtual base class.

              every night, i kneel at the foot of my bed and thank the Great Overseeing Politicians for protecting my freedoms by reducing their number, as if they were deer in a state park. -- Chris Losinger, Online Poker Players?

              1 Reply Last reply
              0
              • S Shog9 0

                Yup.

                every night, i kneel at the foot of my bed and thank the Great Overseeing Politicians for protecting my freedoms by reducing their number, as if they were deer in a state park. -- Chris Losinger, Online Poker Players?

                K Offline
                K Offline
                Kevin McFarlane
                wrote on last edited by
                #8

                It's one of those standard C++ interview questions isn't it? However, I hate questions where they ask you to spot what's wrong with the code and you miss something like this because you tend to see what you want to see. Then if the interviewer pulls you up they can't tell whether you were unaware of the principle or whether you just missed it. I had a question where I was asked what was wrong with a piece of XML and one of the errors was that a closing tag was in the wrong case (one letter of the tag, in fact). I didn't spot it but of course I was perfectly aware that XML is case sensitive!

                Kevin

                1 Reply Last reply
                0
                • L Lost User

                  I got myself a copy of PC Lint (and VisualLint) last week and this would of been picked up - lack of a virtual destructor in the base class is such a common mistake, I'm surprised compiler vendors don't explicitly check for it.


                  Kicking squealing Gucci little piggy.

                  N Offline
                  N Offline
                  Nemanja Trifunovic
                  wrote on last edited by
                  #9

                  Rob Caldecott wrote:

                  lack of a virtual destructor in the base class is such a common mistake, I'm surprised compiler vendors don't explicitly check for it.

                  gcc issues a warning in that case.


                  Programming Blog utf8-cpp

                  1 Reply Last reply
                  0
                  • S Shog9 0

                    Here's another "oh, duh" bug, in the same vein as Rob's:

                    class IWorkItem
                    {
                       public:
                          virtual bool DoWork()=0;
                    };

                    class CSpecificTask : public IWorkItem
                    {
                       public:
                          CSpecificTask(LPCTSTR szName) : m_strName(szName) {}
                          ~CSpecificTask()
                          {
                             //...
                          }
                         
                          virtual bool DoWork()
                          {
                             //...
                          }
                         
                       private:
                          CString m_strName;
                    }

                    //...

                    void AddNewWorkItem()
                    {
                       EnqueueWorkItem(new CSpecificTask(_T("Blah")));
                    }

                    void ProcessWorkItem()
                    {
                       IWorkItem* pItem = PopWorkItem();
                       // ...
                       delete pItem;
                    }

                    Musta stared at memory leak reports for a good two hours last night, before giving up and going to take a shower. As i was washing my face, i realized what i'd missed... :doh: :-O

                    every night, i kneel at the foot of my bed and thank the Great Overseeing Politicians for protecting my freedoms by reducing their number, as if they were deer in a state park. -- Chris Losinger, Online Poker Players?

                    R Offline
                    R Offline
                    Ray Kinsella
                    wrote on last edited by
                    #10

                    Gotta ya, because you haven't explicitly declared a virtual destructor in the base class, the compiler creates a concrete one for you implicitly. So where you call delete against the base class rather than the child class, the child classes destructor never gets called and the memory never gets freed. Nasty nasty nasty ...

                    Regards Ray "Je Suis Mort De Rire" Blogging @ Keratoconus Watch

                    1 Reply Last reply
                    0
                    • S Shog9 0

                      Yup.

                      every night, i kneel at the foot of my bed and thank the Great Overseeing Politicians for protecting my freedoms by reducing their number, as if they were deer in a state park. -- Chris Losinger, Online Poker Players?

                      H Offline
                      H Offline
                      HakunaMatada
                      wrote on last edited by
                      #11

                      Please forgive my ignorance but why does the destructor have to be Virtual?

                      --- :beer: Hakuna-Matada :beer: It means no worries for the rest of your days... It's our problem free, Philosophy :jig: It’s rather simple to write an unmanaged C++ application that crashes when it performs a callback. It’s impossible to write a managed application that does the same, thanks to delegates. - Jeff Prosise

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups