Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Call for Presentations - Qt World Summit

    Solved Get error message double free or corruption (out) when using QMap class

    General and Desktop
    3
    7
    2276
    Loading More Posts
    • 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.
    • H
      HYKim last edited by

      I wanted to use QMap class like this,

      QMap<int,QWidget*> _mapSystemId2vehicleItem;
      

      So I wrote it in my header file like this.

      #ifndef HEADER_H
      #define HEADER_H
      
      #include <QWidget>
      #include <QMap>
      
      class MyClass : public QWidget
      {
          Q_OBJECT
      public:
          MyClass();
          ~MyClass();
      signals:
      
      public slots:
      
      private:
          QWidget*        _childwidget;
      
          QMap<int,QWidget*> _mapSystemId2vehicleItem; //I added this.
      };
      
      #endif // HEADER_H
      

      But when running project exited, I got a message.

      =======================================
      *** Error in `/home/hykim/QtWorkSpace/build-160115_multiVehicleTest-Desktop_Qt_5_5_1_GCC_32bit-Debug/160115_multiVehicleTest': double free or corruption (out): 0x089c2c18 ***
      The program has unexpectedly finished.

      So I erased QMap<int,QWidget*> _mapSystemId2vehicleItem;. and then application didn't put any error.

      I don't know the reason why this error appeared.

      1 Reply Last reply Reply Quote 0
      • JohanSolo
        JohanSolo last edited by

        What is the code in your destructor? I think you deleted your QWidget instances manually, then the parent-children mechanism tries to delete them again.

        `They did not know it was impossible, so they did it.'
        -- Mark Twain

        H 1 Reply Last reply Reply Quote 0
        • H
          HYKim @JohanSolo last edited by

          @JohanSolo
          Nothing in MyClass's destructor.

          MyClass::~MyClass(){
          }
          

          But in MainWindow.cc, ~MyClass is called its destructor.

          MainWindow::~MainWindow()
          {
              myClass->~MyClass();
              delete ui;
          }
          
          1 Reply Last reply Reply Quote 0
          • JohanSolo
            JohanSolo last edited by JohanSolo

            I don't like this line (is it just me?):

            myClass->~MyClass();
            

            If you want to free the allocated memory, why don't you simply call delete myClass?

            Edit:
            I think this is only wiping out the content of you MyClass instance, but the allocated memory is never freed, which then may trigger the error when the MainWindow destructor trips on the myClass pointer.

            `They did not know it was impossible, so they did it.'
            -- Mark Twain

            H 1 Reply Last reply Reply Quote 1
            • H
              HYKim @JohanSolo last edited by

              @JohanSolo Oh, the problem solved.
              Thank you for your advice.

              JohanSolo 1 Reply Last reply Reply Quote 0
              • JohanSolo
                JohanSolo @HYKim last edited by

                @HYKim said:

                @JohanSolo Oh, the problem solved.
                Thank you for your advice.

                You're welcome, I'm glad I could help!

                `They did not know it was impossible, so they did it.'
                -- Mark Twain

                ValentinMichelet 1 Reply Last reply Reply Quote 0
                • ValentinMichelet
                  ValentinMichelet @JohanSolo last edited by ValentinMichelet

                  @JohanSolo said:

                  I don't like this line (is it just me?):

                  myClass->~MyClass();
                  

                  I would like to give my two cents here, for potential interested readers.
                  Calling explicitly a destructor is really rare, and you have to do it very carefully.
                  Basically, you explicitly invoke a destructor when you need to destroy something without freeing the memory (both are done when calling delete). This may happen if you implement a container (a std::vector like for instance) and you provide a pop_back() method: the element has to be destroy while the memory is managed separately.

                  For more information, here is an interesting topic on Stack Overflow:
                  http://stackoverflow.com/questions/16720201/calling-destructor-explicitly

                  1 Reply Last reply Reply Quote 2
                  • First post
                    Last post