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. Remove QSharedPointer from a QList?
QtWS25 Last Chance

Remove QSharedPointer from a QList?

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 4 Posters 832 Views
  • 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.
  • C Offline
    C Offline
    Calicoder
    wrote on last edited by
    #1

    Good day QT'ers, wondering if there's a better way to remove a QSharedPointer from a QList loaded with them. Here's an example:

    void removeData()
    {
            QSharedPointer<DataPoints> dataPoint01(qobject_cast<DataPoints*>(sender()));
            // QList<QSharedPointer<DataPoints>> dataList;
            dataList.removeAll(dataPoint01);
    }
    

    This seems to crash when I run it in VS. I keep getting this error:
    Exception thrown: read access violation.
    this->ptr->**** was 0xFFFFFFFFFFFFFFE7.

    Any ideas? Appreciate the help, thank you!

    jsulmJ 1 Reply Last reply
    0
    • C Calicoder

      Good day QT'ers, wondering if there's a better way to remove a QSharedPointer from a QList loaded with them. Here's an example:

      void removeData()
      {
              QSharedPointer<DataPoints> dataPoint01(qobject_cast<DataPoints*>(sender()));
              // QList<QSharedPointer<DataPoints>> dataList;
              dataList.removeAll(dataPoint01);
      }
      

      This seems to crash when I run it in VS. I keep getting this error:
      Exception thrown: read access violation.
      this->ptr->**** was 0xFFFFFFFFFFFFFFE7.

      Any ideas? Appreciate the help, thank you!

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

      @Calicoder said in Remove QSharedPointer from a QList?:

      sender()

      Sender is a DataPoints?!
      I don't really understand this code snippet.
      removeData() seems to be a slot - is that right?
      If it is a slot who is emitting the signal (which class)?
      Does sender() return a valid pointer (you should always check pointers before using them!)?

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

      C 1 Reply Last reply
      2
      • jsulmJ jsulm

        @Calicoder said in Remove QSharedPointer from a QList?:

        sender()

        Sender is a DataPoints?!
        I don't really understand this code snippet.
        removeData() seems to be a slot - is that right?
        If it is a slot who is emitting the signal (which class)?
        Does sender() return a valid pointer (you should always check pointers before using them!)?

        C Offline
        C Offline
        Calicoder
        wrote on last edited by
        #3

        @jsulm I see that I never do check if the sender does return a valid pointer, great idea. I'll add the check now. Thanks

        1 Reply Last reply
        0
        • C Offline
          C Offline
          Calicoder
          wrote on last edited by
          #4

          Back to crashing when the dataList.removeAll(dataPoint01) is called. I added a if (!dataPoint01.isNull()) check like this:

          void removeData()
          {
                  QSharedPointer<DataPoints> dataPoint01(qobject_cast<DataPoints*>(sender()));
                  // QList<QSharedPointer<DataPoints>> dataList;
                  if (!dataPoint01.isNull())
                  {
                        dataList.removeAll(dataPoint01);
                  }
          }
          

          The check works well and the dataPoint01 variable is not null. The sender is:

          void createDataPoint()
          {
          	QSharedPointer<DataPoints> newDataPoint(new DataPoints());
          	connect(newDataPoint.data(), &Class01::deleteDataPoint, this, &Class02::deleteDataPoint);
          	dataList.append(newDataPoint);
          }
          

          I get this error in VS:

          Exception thrown: read access violation.
          this->ptr->**** was 0xFFFFFFFFFFFFFFE7.
          What could be causing this?

          jeremy_kJ 1 Reply Last reply
          0
          • C Calicoder

            Back to crashing when the dataList.removeAll(dataPoint01) is called. I added a if (!dataPoint01.isNull()) check like this:

            void removeData()
            {
                    QSharedPointer<DataPoints> dataPoint01(qobject_cast<DataPoints*>(sender()));
                    // QList<QSharedPointer<DataPoints>> dataList;
                    if (!dataPoint01.isNull())
                    {
                          dataList.removeAll(dataPoint01);
                    }
            }
            

            The check works well and the dataPoint01 variable is not null. The sender is:

            void createDataPoint()
            {
            	QSharedPointer<DataPoints> newDataPoint(new DataPoints());
            	connect(newDataPoint.data(), &Class01::deleteDataPoint, this, &Class02::deleteDataPoint);
            	dataList.append(newDataPoint);
            }
            

            I get this error in VS:

            Exception thrown: read access violation.
            this->ptr->**** was 0xFFFFFFFFFFFFFFE7.
            What could be causing this?

            jeremy_kJ Offline
            jeremy_kJ Offline
            jeremy_k
            wrote on last edited by jeremy_k
            #5

            @Calicoder said in Remove QSharedPointer from a QList?:

            Back to crashing when the dataList.removeAll(dataPoint01) is called. I added a if (!dataPoint01.isNull()) check like this:

            void removeData()
            {
                    QSharedPointer<DataPoints> dataPoint01(qobject_cast<DataPoints*>(sender()));
                    // QList<QSharedPointer<DataPoints>> dataList;
                    if (!dataPoint01.isNull())
                    {
                          dataList.removeAll(dataPoint01);
                    }
            }
            

            The problem is that this code is creating a QSharedPointer from a raw pointer, which implies ownership of the object pointed to. When the last associated QSharedPointer goes out of scope, the object will be deleted. When removeData() returns, any further attempts to reference the sender is undefined behavior. This will also be a problem if the QObject was not allocated with new. Two QSharedPointers that happen to be initialized with the same raw pointer are not using the same reference count.

            template <typename X> QSharedPointer::QSharedPointer(X *ptr):

            The pointer ptr becomes managed by this QSharedPointer and must not be passed to another QSharedPointer object or deleted outside this object.
            

            Asking a question about code? http://eel.is/iso-c++/testcase/

            C 1 Reply Last reply
            3
            • jeremy_kJ jeremy_k

              @Calicoder said in Remove QSharedPointer from a QList?:

              Back to crashing when the dataList.removeAll(dataPoint01) is called. I added a if (!dataPoint01.isNull()) check like this:

              void removeData()
              {
                      QSharedPointer<DataPoints> dataPoint01(qobject_cast<DataPoints*>(sender()));
                      // QList<QSharedPointer<DataPoints>> dataList;
                      if (!dataPoint01.isNull())
                      {
                            dataList.removeAll(dataPoint01);
                      }
              }
              

              The problem is that this code is creating a QSharedPointer from a raw pointer, which implies ownership of the object pointed to. When the last associated QSharedPointer goes out of scope, the object will be deleted. When removeData() returns, any further attempts to reference the sender is undefined behavior. This will also be a problem if the QObject was not allocated with new. Two QSharedPointers that happen to be initialized with the same raw pointer are not using the same reference count.

              template <typename X> QSharedPointer::QSharedPointer(X *ptr):

              The pointer ptr becomes managed by this QSharedPointer and must not be passed to another QSharedPointer object or deleted outside this object.
              
              C Offline
              C Offline
              Calicoder
              wrote on last edited by
              #6

              @jeremy_k Great explanation thank you. So it would seem passing the newly created DataPoints variable newDataPoint in createDataPoint() as a param in removeData() would be a better solution for this I'm thinking.

              jeremy_kJ 1 Reply Last reply
              0
              • C Calicoder

                @jeremy_k Great explanation thank you. So it would seem passing the newly created DataPoints variable newDataPoint in createDataPoint() as a param in removeData() would be a better solution for this I'm thinking.

                jeremy_kJ Offline
                jeremy_kJ Offline
                jeremy_k
                wrote on last edited by
                #7

                @Calicoder said in Remove QSharedPointer from a QList?:

                @jeremy_k Great explanation thank you. So it would seem passing the newly created DataPoints variable newDataPoint in createDataPoint() as a param in removeData() would be a better solution for this I'm thinking.

                That depends on how it's used. sender() isn't a problem as long as removeData() is called via a direct connection.

                Asking a question about code? http://eel.is/iso-c++/testcase/

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  Hi,

                  Out of curiosity, why do you need QSharedPointer in this case ?

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  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