4

In my code

std::unique_ptr<QNetworkAccessManager> myNetworkAccessManager;
...
myNetworkAccessManager.reset(new QNetworkAccessManager(this));

QObject::connect(myNetworkAccessManager.get(), SIGNAL(finished(QNetworkReply *)), this, SLOT(OnNetworkFinished(QNetworkReply *)));

code in question: myNetworkAccessManager.get() in connect()

My question is what is the best way to do it?

I was reading the article here article where it says under Legacy Code:

Calling get() returns a pointer to the underlying method. You really want to avoid calling this if you can, because as soon as you release that raw pointer into the wild, you have lost much of the advantage you achieved by switching to unique_ptr

Update

If I changed it to use If I used QScopedPointer, my code would be as follows:

QScopedPointer<QNetworkAccessManager> myNetworkAccessManager;
...
myNetworkAccessManager.reset(new QNetworkAccessManager(this));

QObject::connect(myNetworkAccessManager.data(), SIGNAL(finished(QNetworkReply *)), this, SLOT(OnNetworkFinished(QNetworkReply *)));

would this be the correct solution then:

connect(myNetworkAccessManager.data()

Update 2

Reading this link stackoverflowit seems that using data() is the correct solution. Which mean that using get() from the stl is correct also.

Community
  • 1
  • 1
adviner
  • 3,295
  • 10
  • 35
  • 64

1 Answers1

4

Qt has it's own memory management mechanism, so using std::unique_ptr for objects that are handled with that mechanism is not correct. I would advice to simply be sure to define parent-child relations correctly and let Qt handle that for you.

Shoe
  • 74,840
  • 36
  • 166
  • 272
  • I updated my code to use QScopedPointer and wondering if the use of data() is correct in connect() – adviner Oct 23 '15 at 03:29
  • As @Jefffrey pointed out, Qt has its own memory management but to get the pointer to the object using the get() or data() is not wrong. Since the object must be a QObject for the connect, Qt just use the pointer to monitor the lifetime of the object, it does not try to take ownership. There are other places where this is definitely not correct but for the signals and slots it should work in most cases without issues. Get familiar with [std::enabled_shared_from_this](http://en.cppreference.com/w/cpp/memory/enable_shared_from_this) to get the smart pointer in the slot. – CJCombrink Oct 23 '15 at 06:17
  • Given this point has never been corrected in all these years, keep in mind that not all Qt objects are handled by Qt's memory management mechanism. Oddly enough, [Qt's doc on Object Trees & Ownership](https://doc.qt.io/qt-5/objecttrees.html) shows examples of objects that have no parent, specially the typical example of declaring them in the stack. Given that the stack has a very limited capacity (by default, 1MB on Windows and around 8MB on linux/macOS) then it's customary to declare root objects in the heap with its life cycle managed by smart pointers such as `std::unique_ptr`. – RAM Jan 23 '22 at 23:27