Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-49824

QSharedPointer::create calls destructor when an exception is thrown

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Done
    • Icon: P3: Somewhat important P3: Somewhat important
    • 5.9.1
    • 5.4.2
    • None
    • Windows 7, MSVC 2012
    • d1210281e41008ce2e3510aa5cfb3ebea1c57734

      QSharedPointer::create() improperly calls the destructor when the constructor throws an exception.

      #include <QSharedPointer>
      
      class MyClass
      {
      public:
          MyClass()
          {
              qDebug("MyClass");
              throw std::exception();
          }
      
          ~MyClass()
          {
              qDebug("~MyClass()");
          }
      };
      
      int main(int argc, char *argv[])
      {
          try
          {
              QSharedPointer<MyClass> withCreate = QSharedPointer<MyClass>::create();
          }
          catch (const std::exception&)
          {
          }
      
          try
          {
              QSharedPointer<MyClass> withoutCreate( new MyClass() );
          }
          catch (const std::exception&)
          {
          }
      }
      

      The output is:

      MyClass()
      ~MyClass()
      MyClass()
      

      The output should be:

      MyClass()
      MyClass()
      

        For Gerrit Dashboard: QTBUG-49824
        # Subject Branch Project Status CR V

            thiago Thiago Macieira
            keranen Eric Keranen
            Votes:
            2 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved:

                There are no open Gerrit changes