<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[unique_ptr question]]></title><description><![CDATA[<p dir="auto">Hello, I have 2 slots:</p>
<pre><code>void Location::getGpsLocation() {
    unique_ptr&lt;QGeoPositionInfoSource&gt; posInfoSource(QGeoPositionInfoSource::createDefaultSource(0));
    if (posInfoSource) {
        QThread *thread = new QThread();
        posInfoSource.get()-&gt;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-&gt;start();
    }
    else {
        qDebug() &lt;&lt; "No position info source available.";
    }
}
</code></pre>
<p dir="auto">and</p>
<pre><code>void Location::locationPositionInfo(const QGeoPositionInfo &amp;posInfo) {
    QThread *thread = new QThread();
    unique_ptr&lt;LocationController&gt; controller(new LocationController(0));
    controller.get()-&gt;m_lat = posInfo.coordinate().latitude();
    controller.get()-&gt;m_lon = posInfo.coordinate().longitude();
    controller.get()-&gt;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-&gt;start();
}
</code></pre>
<p dir="auto">As you can see I am using unique_ptr for both QGeoPositionInfoSource and LocationController.<br />
The thing is, locationPositionInfo(const QGeoPositionInfo &amp;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-&gt;() start so it confuses me because posInfoSource pointer is not deleted.<br />
Shouldn't unique_ptr delete the pointer at the end of the scope?<br />
That said, am I understanding scopes wrongly? Isn't the scope the function's body?</p>
]]></description><link>https://forum.qt.io/topic/90872/unique_ptr-question</link><generator>RSS for Node</generator><lastBuildDate>Fri, 19 Jun 2026 22:46:00 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/90872.rss" rel="self" type="application/rss+xml"/><pubDate>Sat, 19 May 2018 11:25:08 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to unique_ptr question on Fri, 25 May 2018 15:01:05 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/vronin">@<bdi>VRonin</bdi></a> Oh, I was not taking unique_ptr into account as I removed it from my code.</p>
<p dir="auto">Thanks for your input though!</p>
]]></description><link>https://forum.qt.io/post/460257</link><guid isPermaLink="true">https://forum.qt.io/post/460257</guid><dc:creator><![CDATA[adutzu89]]></dc:creator><pubDate>Fri, 25 May 2018 15:01:05 GMT</pubDate></item><item><title><![CDATA[Reply to unique_ptr question on Tue, 22 May 2018 17:10:26 GMT]]></title><description><![CDATA[<blockquote>
<p dir="auto">How come? Can you elaborate, please?</p>
</blockquote>
<ul>
<li><code>Location::locationPositionInfo</code> is run in the main thread</li>
<li><code>controller.get()</code> lives in <code>thread</code></li>
<li><code>unique_ptr</code>'s destructor calls <code>delete</code> from <code>Location::locationPositionInfo</code></li>
</ul>
<blockquote>
<p dir="auto">am I understanding scopes wrongly?</p>
</blockquote>
<p dir="auto">I think you are</p>
<blockquote>
<p dir="auto">Isn't the scope the function's body?</p>
</blockquote>
<p dir="auto">Yes, the body of <code>locationPositionInfo</code> and <code>getGpsLocation</code></p>
]]></description><link>https://forum.qt.io/post/459624</link><guid isPermaLink="true">https://forum.qt.io/post/459624</guid><dc:creator><![CDATA[VRonin]]></dc:creator><pubDate>Tue, 22 May 2018 17:10:26 GMT</pubDate></item><item><title><![CDATA[Reply to unique_ptr question on Mon, 21 May 2018 16:10:09 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/kshegunov">@<bdi>kshegunov</bdi></a> Thanks for your answer.</p>
<blockquote>
<p dir="auto">Something more you're deleting objects from the incorrect thread</p>
</blockquote>
<p dir="auto">How come? Can you elaborate, please?<br />
I am using thread's signal to delee the objects through deleteLater() slot which from the documentation it says I should be using.</p>
<p dir="auto">From <a href="http://doc.qt.io/qt-5/qthread.html#finished" target="_blank" rel="noopener noreferrer nofollow ugc">QThread::finished()</a>:</p>
<blockquote>
<p dir="auto">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.</p>
</blockquote>
<blockquote>
<p dir="auto">which should also give you warnings in the application output pane when the code is run.</p>
</blockquote>
<p dir="auto">There aren't any warnings related to thread in the output pane.</p>
]]></description><link>https://forum.qt.io/post/459412</link><guid isPermaLink="true">https://forum.qt.io/post/459412</guid><dc:creator><![CDATA[adutzu89]]></dc:creator><pubDate>Mon, 21 May 2018 16:10:09 GMT</pubDate></item><item><title><![CDATA[Reply to unique_ptr question on Sat, 19 May 2018 20:17:56 GMT]]></title><description><![CDATA[<p dir="auto">Both snippets do nothing, as your objects are owned by the <code>unique_ptr</code> 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!</p>
<blockquote>
<p dir="auto">The thing is, locationPositionInfo(const QGeoPositionInfo &amp;posInfo) is getting called</p>
</blockquote>
<p dir="auto">It's pure luck that the thread is started and processed one of the slots before the object got deleted, nothing else.</p>
]]></description><link>https://forum.qt.io/post/459177</link><guid isPermaLink="true">https://forum.qt.io/post/459177</guid><dc:creator><![CDATA[kshegunov]]></dc:creator><pubDate>Sat, 19 May 2018 20:17:56 GMT</pubDate></item></channel></rss>