Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. unique_ptr question
Qt 6.11 is out! See what's new in the release blog

unique_ptr question

Scheduled Pinned Locked Moved Unsolved C++ Gurus
5 Posts 3 Posters 2.4k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    adutzu89
    wrote on last edited by
    #1

    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?

    kshegunovK 1 Reply Last reply
    0
    • A adutzu89

      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?

      kshegunovK Offline
      kshegunovK Offline
      kshegunov
      Moderators
      wrote on last edited by kshegunov
      #2

      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.

      Read and abide by the Qt Code of Conduct

      A 1 Reply Last reply
      5
      • kshegunovK kshegunov

        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.

        A Offline
        A Offline
        adutzu89
        wrote on last edited by
        #3

        @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.

        1 Reply Last reply
        0
        • VRoninV Offline
          VRoninV Offline
          VRonin
          wrote on last edited by
          #4

          How come? Can you elaborate, please?

          • Location::locationPositionInfo is run in the main thread
          • controller.get() lives in thread
          • unique_ptr's destructor calls delete from Location::locationPositionInfo

          am I understanding scopes wrongly?

          I think you are

          Isn't the scope the function's body?

          Yes, the body of locationPositionInfo and getGpsLocation

          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
          ~Napoleon Bonaparte

          On a crusade to banish setIndexWidget() from the holy land of Qt

          A 1 Reply Last reply
          4
          • VRoninV VRonin

            How come? Can you elaborate, please?

            • Location::locationPositionInfo is run in the main thread
            • controller.get() lives in thread
            • unique_ptr's destructor calls delete from Location::locationPositionInfo

            am I understanding scopes wrongly?

            I think you are

            Isn't the scope the function's body?

            Yes, the body of locationPositionInfo and getGpsLocation

            A Offline
            A Offline
            adutzu89
            wrote on last edited by
            #5

            @VRonin Oh, I was not taking unique_ptr into account as I removed it from my code.

            Thanks for your input though!

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved