Unsolved unique_ptr question
-
Hello, I have 2 slots:
void Location::getGpsLocation() { unique_ptr<QGeoPositionInfoSource> posInfoSource(QGeoPositionInfoSource::createDefaultSource(0)); if (posInfoSource) { QThread *thread = new QThread(); posInfoSource.get()->moveToThread(thread); connect(thread, SIGNAL(started()), posInfoSource.get(), SLOT(startUpdates())); connect(posInfoSource.get(), SIGNAL(positionUpdated(QGeoPositionInfo)), this, SLOT(locationPositionInfo(QGeoPositionInfo))); connect(posInfoSource.get(), SIGNAL(positionUpdated(QGeoPositionInfo)), thread, SLOT(quit())); connect(posInfoSource.get(), SIGNAL(error(QGeoPositionInfoSource::Error)), thread, SLOT(quit())); connect(thread, SIGNAL(finished()), posInfoSource.get(), SLOT(stopUpdates())); connect(thread, SIGNAL(finished()), posInfoSource.get(), SLOT(deleteLater())); connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater())); thread->start(); } else { qDebug() << "No position info source available."; } }
and
void Location::locationPositionInfo(const QGeoPositionInfo &posInfo) { QThread *thread = new QThread(); unique_ptr<LocationController> controller(new LocationController(0)); controller.get()->m_lat = posInfo.coordinate().latitude(); controller.get()->m_lon = posInfo.coordinate().longitude(); controller.get()->moveToThread(thread); connect(thread, SIGNAL(started()), controller.get(), SLOT(searchByCoordinates())); connect(controller.get(), SIGNAL(networkError(QString)), this, SIGNAL(networkError(QString))); connect(controller.get(), SIGNAL(locationFromGps(QString)), this, SLOT(setGpsLocation(QString))); connect(thread, SIGNAL(finished()), controller.get(), SLOT(deleteLater())); connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater())); connect(this, SIGNAL(networkError(QString)), thread, SLOT(quit())); connect(this, SIGNAL(gpsLocationChanged()), thread, SLOT(quit())); thread->start(); }
As you can see I am using unique_ptr for both QGeoPositionInfoSource and LocationController.
The thing is, locationPositionInfo(const QGeoPositionInfo &posInfo) is getting called while searchByCoordinates() isn't called after thread is started, which leads me to believe the controller pointer is getting deleted after thread->() start so it confuses me because posInfoSource pointer is not deleted.
Shouldn't unique_ptr delete the pointer at the end of the scope?
That said, am I understanding scopes wrongly? Isn't the scope the function's body? -
Both snippets do nothing, as your objects are owned by the
unique_ptr
and are getting deleted when the method goes out of scope. Something more you're deleting objects from the incorrect thread, which should also give you warnings in the application output pane when the code is run. Don't ignore those warnings, treat them as errors!The thing is, locationPositionInfo(const QGeoPositionInfo &posInfo) is getting called
It's pure luck that the thread is started and processed one of the slots before the object got deleted, nothing else.
-
@kshegunov Thanks for your answer.
Something more you're deleting objects from the incorrect thread
How come? Can you elaborate, please?
I am using thread's signal to delee the objects through deleteLater() slot which from the documentation it says I should be using.From QThread::finished():
When this signal is emitted, the event loop has already stopped running. No more events will be processed in the thread, except for deferred deletion events. This signal can be connected to QObject::deleteLater(), to free objects in that thread.
which should also give you warnings in the application output pane when the code is run.
There aren't any warnings related to thread in the output pane.
-
How come? Can you elaborate, please?
Location::locationPositionInfo
is run in the main threadcontroller.get()
lives inthread
unique_ptr
's destructor callsdelete
fromLocation::locationPositionInfo
am I understanding scopes wrongly?
I think you are
Isn't the scope the function's body?
Yes, the body of
locationPositionInfo
andgetGpsLocation
-
@VRonin Oh, I was not taking unique_ptr into account as I removed it from my code.
Thanks for your input though!