#include #include #include #include #include #include QMutex mutex; class Storage { public: Storage() { x=new int; mutex.lock(); std::cout << "Allocated int at " << (int)x << "\n"; mutex.unlock(); } ~Storage() { mutex.lock(); std::cout << "Destructor: deallocating int at " << (int)x << "\n"; mutex.unlock(); delete x; } Storage(const Storage &p) { x = new int(*(p.x)); mutex.lock(); std::cout << "Copy constructor: allocated int at " << (int)x << "\n"; mutex.unlock(); } Storage& operator=(const Storage& p) { if (this != &p) // protect against self-assignment { *x = *(p.x); } return *this; } void setValue(int p) { *x=p; } int getValue() const { return *x; } private: int *x; }; Storage compute() { Storage res; res.setValue(3); return res; } int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); { // QFutureWatcher watcher; // This is OK QFutureWatcher watcher; // This causes a memory leak QFuture future = QtConcurrent::run(compute); watcher.setFuture(future); watcher.waitForFinished(); Storage k(future.resultAt(0)); mutex.lock(); std::cout << "Computation result: " << k.getValue() << "\n"; mutex.unlock(); } return a.exec(); }