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 Usage Found
Forum Updated to NodeBB v4.3 + New Features

QSharedPointer Usage Found

Scheduled Pinned Locked Moved General and Desktop
qsharedpointer
8 Posts 4 Posters 6.7k Views 3 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.
  • S Offline
    S Offline
    sharethl
    wrote on last edited by SGaist
    #1

    Found that if take object out of shared pointer, and put it into a new shared pointer, will cause crash.
    The following code will crash after j=1, i=0.
    Is is expected?

    I wrote a page here, with pictures, may be easier to see.
    link text

    Thanks

    #include "mainwindow.h"
    #include <QApplication>
    #include <QDebug>

    class Animal
    {
    public:
    Animal(int id){this->id = id;}
    virtual ~Animal(){}
    int id;
    };
    class Dog:public Animal
    {
    public:
    Dog(int id):Animal(id){}
    ~Dog(){}
    int legs;
    };

    int main(int argc, char *argv[])
    {

    QList<QSharedPointer<Animal>> animals;
    for(int i=0; i<10; i++){
        animals.append(QSharedPointer<Animal>(new Dog(i)));
    }
    for(int j =0; j < 100; j++){
        for(int i=0; i< 10; i++){
            qDebug() << "j=" << j << "i=" << i;
            // take out data and assign it to a new shared pointer
            auto p = QSharedPointer<Animal>(animals[i].data());
        }
    }
    QApplication a(argc, argv);
    MainWindow w;
    w.show();
    return a.exec();
    

    }
    Hope it helps

    1 Reply Last reply
    0
    • A Offline
      A Offline
      alex_malyu
      wrote on last edited by
      #2

      auto p = QSharedPointer<Animal>(animals[i].data());

      Above code means that p is going out of scope.
      and thus pointer will be deleted.

      Documentation clearly states:

      T * QSharedPointer::data () const
      Returns the value of the pointer referenced by this object.

      Note: do not delete the pointer returned by this function or pass it to another function that could delete it, including creating QSharedPointer or QWeakPointer objects.

      A 1 Reply Last reply
      0
      • A alex_malyu

        auto p = QSharedPointer<Animal>(animals[i].data());

        Above code means that p is going out of scope.
        and thus pointer will be deleted.

        Documentation clearly states:

        T * QSharedPointer::data () const
        Returns the value of the pointer referenced by this object.

        Note: do not delete the pointer returned by this function or pass it to another function that could delete it, including creating QSharedPointer or QWeakPointer objects.

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

        @alex_malyu However, docs also say:
        "QSharedPointer will delete the pointer it is holding when it goes out of scope, provided no other QSharedPointer objects are referencing it."

        As far as I see, the list still holds a shared pointer to all objects, so none of them should be deleted.

        1 Reply Last reply
        0
        • Chris KawaC Offline
          Chris KawaC Offline
          Chris Kawa
          Lifetime Qt Champion
          wrote on last edited by Chris Kawa
          #4

          There's no magic to shared pointers. The pointer returned by data() does not carry information about the original shared pointer (how could it?). Your code creates two shared pointers with reference count 1. If you want to really share the data use copy constructor or operator= e.g.

          auto p = animals[i];
          

          This will cause the p to have ref count 2 and thus it will not delete the data when p goes out of scope.

          1 Reply Last reply
          1
          • S Offline
            S Offline
            sharethl
            wrote on last edited by
            #5

            Agree with Asperamanca said.
            Chris Kawa, Yes, we could use second shared pointer as you said. But wonder why my way will crash.

            A 1 Reply Last reply
            0
            • S sharethl

              Agree with Asperamanca said.
              Chris Kawa, Yes, we could use second shared pointer as you said. But wonder why my way will crash.

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

              @sharethl Chris explained it. You create two shared pointers, which don't know anything about each other. The pointer "p" runs out of scope, believes it held the last instance to the object and deletes it. On the next loop, you attempt to create a shared pointer from a deleted object.

              1 Reply Last reply
              0
              • Chris KawaC Offline
                Chris KawaC Offline
                Chris Kawa
                Lifetime Qt Champion
                wrote on last edited by
                #7

                @Asperamanca Just to clarify - creating another shared pointer from the deleted pointer is ok. Well.. not really ok, but it will not cause a crash. The crash will occur when the original shared pointer (the one in the array) or the new one go out of scope, at which point they would call delete on an invalid pointer.

                1 Reply Last reply
                0
                • S Offline
                  S Offline
                  sharethl
                  wrote on last edited by sharethl
                  #8

                  Yes, Chris. That is right, once use shared pointer, never delete it.

                  This is the QSharedPointer.data() document in help. see content in []

                  do not delete the pointer returned by this function or pass it to another function that could delete it, [including creating QSharedPointer or QWeakPointer objects.]

                  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