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

QSharedPointer<T>::create calls T::~T even when T::T throws

    XMLWordPrintable

Details

    • Bug
    • Resolution: Duplicate
    • P4: Low
    • None
    • 4.8.x, 5.5.1, 5.6.0
    • Core: Other
    • None

    Description

      QSharedPointer<T>::create(...) can be used as the Qt equivalent of std::make_shared<T>(...). These functions call T's constructor automatically for the user.

      When the constructor throws an exception, the compiler will itself ensure to leave the object in a destructed state so that QSharedPointer<T>::create(...) should not manually call T::~T when the construction failed by an exception. However, that's what it does and it's highly likely to crash or misbehave in other ways for every non-trivial real-world situation.

      class X {
      public:
         X() { throw 42; }
         ~X() { std::cout << "~X called!"; }
      };
      
      int main() {
         try {
            QSharedPointer<X>::create();
         } catch(...) {
            std::cout << "catched!";
         }
      }
      

      Expected output: "catched!". Actual output: "~X called!catched".

      Attachments

        Issue Links

          No reviews matched the request. Check your Options in the drop-down menu of this sections header.

          Activity

            People

              thiago Thiago Macieira
              litb Johannes Schaub
              Votes:
              2 Vote for this issue
              Watchers:
              6 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:

                Gerrit Reviews

                  There are no open Gerrit changes