https://gcc.gnu.org/bugzilla/show_bug.cgi?id=91434

Rajabharathi s <rajawrite at gmail dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|RESOLVED                    |UNCONFIRMED
         Resolution|INVALID                     |---

--- Comment #3 from Rajabharathi s <rajawrite at gmail dot com> ---
 (In reply to Jonathan Wakely from comment #2)
> This code has undefined behaviour:
> 
> int main()
> {
>   FSPI pi;
>   FSPG* pg = new FSPG();
>   new (&pi.m_a) FSPGI(pg);
>   pi.m_a.~FSPGI();
>   return 0;
> }
> 
> pi.m_a is part of an object on the stack, so its destructor will run
> automatically. If you invoke it explicitly as pi.m_a.~FSPGI() then you cause
> the same object to be destroyed twice. This is undefined. You need to fix
> your code.


Thanks for your reply. I understand this.
But since m_a is member variable of other object FSPI, this memory exists in
stack until its destruction.

m_g is set to NULL in destructor part when called for first time.
So second time, this if condition should be false.
If -fno-tree-dse option is used, then this if condition is false and works fine
in 4.9.4.

  ~FSPGI()
      {cout << "FSPGI dtor\n";

         if(NULL != m_g)
         {
          cout << "m_g is not NULL\n";
          delete m_g;
          m_g = NULL;
         }
      }

 class  FSPI
      {

      public:
      FSPI()
      {
        cout << "FSPI ctor\n";
      }
      ~FSPI()
      {
        cout << "FSPI dtor \n";
      }
      FSPGI m_a;
      };

Reply via email to