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. General Programming
  3. C / C++ / MFC
  4. Problem with throwing class objects

Problem with throwing class objects

Scheduled Pinned Locked Moved C / C++ / MFC
helpc++questionlearning
4 Posts 2 Posters 0 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.
  • M Offline
    M Offline
    Malcolm McMahon
    wrote on last edited by
    #1

    I seem to have stubled across my second bug (in about 8 hours worth of using this compiler). Now, I've always used what I believed to be the standard way of throwing class exceptions (as shown in Stroustrup): class AWobbley { char *m_msg; public: AWobbley(const char *msg); ... }; ... throw AWobbley("AGHRR!"); ... catch (AWobbley &e) { ... do something with e.m_msg } The problem came up when the exception class stored a dynamically constructed message and, thus, needed a destructor to delete it. In VC++ this destructor is called before the exception handler is entered. This, of course, completely messes up the whole technique.:mad: I haven't yet found a bug report on the Microsoft site. Anyone else with this problem? Yes, of course there are work arrounds like passing a pointer to a newed object and deleting it at the bottom of the exception handler, but it's ugly and something I wouldn't want to keep for another compiler.

    T 1 Reply Last reply
    0
    • M Malcolm McMahon

      I seem to have stubled across my second bug (in about 8 hours worth of using this compiler). Now, I've always used what I believed to be the standard way of throwing class exceptions (as shown in Stroustrup): class AWobbley { char *m_msg; public: AWobbley(const char *msg); ... }; ... throw AWobbley("AGHRR!"); ... catch (AWobbley &e) { ... do something with e.m_msg } The problem came up when the exception class stored a dynamically constructed message and, thus, needed a destructor to delete it. In VC++ this destructor is called before the exception handler is entered. This, of course, completely messes up the whole technique.:mad: I haven't yet found a bug report on the Microsoft site. Anyone else with this problem? Yes, of course there are work arrounds like passing a pointer to a newed object and deleting it at the bottom of the exception handler, but it's ugly and something I wouldn't want to keep for another compiler.

      T Offline
      T Offline
      Tomasz Sowinski
      wrote on last edited by
      #2

      You need to add the copy constructor to AWobbley and program will behave as expected. If you comment out AWobbley(const AWobbley &a), you'll see two dtor calls and only one ctor - looks like a bug. And the funniest thing is that program doesn't call copy ctor when it is defined.

      class AWobbley
      {
      public:
      AWobbley() { printf("def ctor\n"); }
      AWobbley(const char *msg) : m_msg(msg) { printf("ctor\n");}
      AWobbley(const AWobbley &a) { printf("copy ctor\n"); m_msg = a.m_msg; }
      ~AWobbley() { printf("dtor\n"); }
      void print() { printf("%s\n", m_msg); }
      private:
      const char *m_msg;
      };

      void main( void )
      {
      try
      {
      throw AWobbley("AGHRR!");
      }
      catch (AWobbley &e)
      {
      printf("exception handler\n");
      e.print();
      }
      }

      Tomasz Sowinski -- http://www.shooltz.com

      M 1 Reply Last reply
      0
      • T Tomasz Sowinski

        You need to add the copy constructor to AWobbley and program will behave as expected. If you comment out AWobbley(const AWobbley &a), you'll see two dtor calls and only one ctor - looks like a bug. And the funniest thing is that program doesn't call copy ctor when it is defined.

        class AWobbley
        {
        public:
        AWobbley() { printf("def ctor\n"); }
        AWobbley(const char *msg) : m_msg(msg) { printf("ctor\n");}
        AWobbley(const AWobbley &a) { printf("copy ctor\n"); m_msg = a.m_msg; }
        ~AWobbley() { printf("dtor\n"); }
        void print() { printf("%s\n", m_msg); }
        private:
        const char *m_msg;
        };

        void main( void )
        {
        try
        {
        throw AWobbley("AGHRR!");
        }
        catch (AWobbley &e)
        {
        printf("exception handler\n");
        e.print();
        }
        }

        Tomasz Sowinski -- http://www.shooltz.com

        M Offline
        M Offline
        Malcolm McMahon
        wrote on last edited by
        #3

        Now that is weird. I'd say definitely a compiler bug. Thanks for the work arround. :rose:

        T 1 Reply Last reply
        0
        • M Malcolm McMahon

          Now that is weird. I'd say definitely a compiler bug. Thanks for the work arround. :rose:

          T Offline
          T Offline
          Tomasz Sowinski
          wrote on last edited by
          #4

          The stuff that happens without copy ctor looks like bug. Behavior with copy ctor defined may be optimization - the exception object is created 'in-place' once. Tomasz Sowinski -- http://www.shooltz.com

          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