Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QSharedPointer reset()
Forum Updated to NodeBB v4.3 + New Features

QSharedPointer reset()

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 3 Posters 365 Views 1 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.
  • R Offline
    R Offline
    Redman
    wrote on last edited by Redman
    #1
    qDebug() << "START";
    for (int i = 0; i < 5; i++) {
       if (list.length() >= 1)
          qDebug() << list.last().data()->objectName();
    
       QSharedPointer<QObject> tmp = QSharedPointer<QObject>(new QObject());
       tmp.data()->setObjectName("Test");
    
       list.append(QSharedPointer<QObject>(new QObject()));
       list.last().reset(tmp.data());
    }
    qDebug() << "END";
    

    Please explain to me, why the objectName 'Test' is lost on the list entry. When tmp is destructed the name gets lost, even though I set the list entry to point to the same QObject

    Btw, when qDebug() is called, the program crashes

    A jsulmJ 2 Replies Last reply
    0
    • R Redman
      qDebug() << "START";
      for (int i = 0; i < 5; i++) {
         if (list.length() >= 1)
            qDebug() << list.last().data()->objectName();
      
         QSharedPointer<QObject> tmp = QSharedPointer<QObject>(new QObject());
         tmp.data()->setObjectName("Test");
      
         list.append(QSharedPointer<QObject>(new QObject()));
         list.last().reset(tmp.data());
      }
      qDebug() << "END";
      

      Please explain to me, why the objectName 'Test' is lost on the list entry. When tmp is destructed the name gets lost, even though I set the list entry to point to the same QObject

      Btw, when qDebug() is called, the program crashes

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #3

      @Redman The problem is that after reset you have two different shared pointers pointing to same data. As soon as tmp is deleted (on next loop iteration) it deletes the data it points to. Instead of reset use swap(...).

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      2
      • R Redman
        qDebug() << "START";
        for (int i = 0; i < 5; i++) {
           if (list.length() >= 1)
              qDebug() << list.last().data()->objectName();
        
           QSharedPointer<QObject> tmp = QSharedPointer<QObject>(new QObject());
           tmp.data()->setObjectName("Test");
        
           list.append(QSharedPointer<QObject>(new QObject()));
           list.last().reset(tmp.data());
        }
        qDebug() << "END";
        

        Please explain to me, why the objectName 'Test' is lost on the list entry. When tmp is destructed the name gets lost, even though I set the list entry to point to the same QObject

        Btw, when qDebug() is called, the program crashes

        A Offline
        A Offline
        Asperamanca
        wrote on last edited by
        #2

        @Redman
        Not sure about the object name. But the code is suspicious in any case: Both tmp and list.last() are now owners of the same object. I think what you want is list.last() = tmp, no?

        1 Reply Last reply
        0
        • R Redman
          qDebug() << "START";
          for (int i = 0; i < 5; i++) {
             if (list.length() >= 1)
                qDebug() << list.last().data()->objectName();
          
             QSharedPointer<QObject> tmp = QSharedPointer<QObject>(new QObject());
             tmp.data()->setObjectName("Test");
          
             list.append(QSharedPointer<QObject>(new QObject()));
             list.last().reset(tmp.data());
          }
          qDebug() << "END";
          

          Please explain to me, why the objectName 'Test' is lost on the list entry. When tmp is destructed the name gets lost, even though I set the list entry to point to the same QObject

          Btw, when qDebug() is called, the program crashes

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #3

          @Redman The problem is that after reset you have two different shared pointers pointing to same data. As soon as tmp is deleted (on next loop iteration) it deletes the data it points to. Instead of reset use swap(...).

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          2
          • R Redman has marked this topic as solved on
          • R Redman has marked this topic as unsolved on
          • R Redman has marked this topic as solved on

          • Login

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