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. Get error message double free or corruption (out) when using QMap class
QtWS25 Last Chance

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

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 3 Posters 2.9k 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.
  • H Offline
    H Offline
    HYKim
    wrote on last edited by
    #1

    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
    0
    • JohanSoloJ Offline
      JohanSoloJ Offline
      JohanSolo
      wrote on last edited by
      #2

      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
      0
      • JohanSoloJ JohanSolo

        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.

        H Offline
        H Offline
        HYKim
        wrote on last edited by
        #3

        @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
        0
        • JohanSoloJ Offline
          JohanSoloJ Offline
          JohanSolo
          wrote on last edited by JohanSolo
          #4

          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
          1
          • JohanSoloJ 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.

            H Offline
            H Offline
            HYKim
            wrote on last edited by
            #5

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

            JohanSoloJ 1 Reply Last reply
            0
            • H HYKim

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

              JohanSoloJ Offline
              JohanSoloJ Offline
              JohanSolo
              wrote on last edited by
              #6

              @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

              ValentinMicheletV 1 Reply Last reply
              0
              • JohanSoloJ JohanSolo

                @HYKim said:

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

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

                ValentinMicheletV Offline
                ValentinMicheletV Offline
                ValentinMichelet
                wrote on last edited by ValentinMichelet
                #7

                @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
                2

                • Login

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