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 crash in Linux
Qt 6.11 is out! See what's new in the release blog

QSharedPointer crash in Linux

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 2 Posters 840 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.
  • A Offline
    A Offline
    aliks-os
    wrote on last edited by aliks-os
    #1

    The code at below works fine in Windows, but crash in Linux. Please advice what I do wrong and how the problem can be fixed.
    I get the following error message
    free(): invalid pointer

    class Process {
    
    public:
       Process();
       MyStruct m_data;
    };
    
    Process::Process()
    {
       QSharedPointer<MyStruct>  dataPtr = QSharedPointer<MyStruct>(&m_data);   <---Here crash
    }
    
    1 Reply Last reply
    0
    • jeremy_kJ Offline
      jeremy_kJ Offline
      jeremy_k
      wrote on last edited by
      #2

      Why are you creating a shared pointer to a class member variable?

      From the documentation:
      QSharedPointer will delete the pointer it is holding when it goes out of scope, provided no other QSharedPointer objects are referencing it.

      When the two QSharedPointer instances in Process::Process() go out of scope at the end of the function, delete will be called on &m_data, which wasn't allocated with new.

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

      A 1 Reply Last reply
      3
      • jeremy_kJ jeremy_k

        Why are you creating a shared pointer to a class member variable?

        From the documentation:
        QSharedPointer will delete the pointer it is holding when it goes out of scope, provided no other QSharedPointer objects are referencing it.

        When the two QSharedPointer instances in Process::Process() go out of scope at the end of the function, delete will be called on &m_data, which wasn't allocated with new.

        A Offline
        A Offline
        aliks-os
        wrote on last edited by aliks-os
        #3

        @jeremy_k said in QSharedPointer crash in Linux:

        Why are you creating a shared pointer to a class member variable?

        So the correct approach will be as below, rigth?:

        class Process {
        
        public:
           Process();
           MyStruct *m_data;
        };
        
        Process::Process()
        {
           m_data = new MyStruct();
           QSharedPointer<MyStruct>  dataPtr = QSharedPointer<MyStruct>(m_data);
        }
        
        jeremy_kJ 1 Reply Last reply
        1
        • A aliks-os

          @jeremy_k said in QSharedPointer crash in Linux:

          Why are you creating a shared pointer to a class member variable?

          So the correct approach will be as below, rigth?:

          class Process {
          
          public:
             Process();
             MyStruct *m_data;
          };
          
          Process::Process()
          {
             m_data = new MyStruct();
             QSharedPointer<MyStruct>  dataPtr = QSharedPointer<MyStruct>(m_data);
          }
          
          jeremy_kJ Offline
          jeremy_kJ Offline
          jeremy_k
          wrote on last edited by
          #4

          @aliks-os said in QSharedPointer crash in Linux:

          @jeremy_k said in QSharedPointer crash in Linux:

          Why are you creating a shared pointer to a class member variable?
          

          So the correct approach will be as below, rigth?:

          class Process {
          
          public:
             Process();
             MyStruct *m_data;
          };
          
          Process::Process()
          {
             m_data = new MyStruct();
             QSharedPointer<MyStruct>  dataPtr = QSharedPointer<MyStruct>(m_data);
          }
          

          That depends on the goal, but I would guess not. As is, the instance of MyStruct will be created and deleted in the Process constructor. At that point, m_data will be a dangling pointer. It might be easier to understand a simplified form:

          MyStruct *dangling;
          void f()
          {
            QSharedPointer<MyStruct> dataPtr(new MyStruct);
            dangling = dataPtr.data();
            // MyStruct instance deleted here
            // dangling doesn't point to a valid object
          }
          

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

          A 1 Reply Last reply
          2
          • jeremy_kJ jeremy_k

            @aliks-os said in QSharedPointer crash in Linux:

            @jeremy_k said in QSharedPointer crash in Linux:

            Why are you creating a shared pointer to a class member variable?
            

            So the correct approach will be as below, rigth?:

            class Process {
            
            public:
               Process();
               MyStruct *m_data;
            };
            
            Process::Process()
            {
               m_data = new MyStruct();
               QSharedPointer<MyStruct>  dataPtr = QSharedPointer<MyStruct>(m_data);
            }
            

            That depends on the goal, but I would guess not. As is, the instance of MyStruct will be created and deleted in the Process constructor. At that point, m_data will be a dangling pointer. It might be easier to understand a simplified form:

            MyStruct *dangling;
            void f()
            {
              QSharedPointer<MyStruct> dataPtr(new MyStruct);
              dangling = dataPtr.data();
              // MyStruct instance deleted here
              // dangling doesn't point to a valid object
            }
            
            A Offline
            A Offline
            aliks-os
            wrote on last edited by
            #5

            @jeremy_k
            yes you are right. Many thanks for your help. I have understood my mistake. In my case better to restruct the workflow of my code.
            But I still wondering why the first version of my code works in windows but not in linux. )))

            1 Reply Last reply
            0
            • jeremy_kJ Offline
              jeremy_kJ Offline
              jeremy_k
              wrote on last edited by
              #6

              Chance plus the choices made by the compiler and runtime sometimes appear to produce working programs for undefined behavior.

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

              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