Important: Please read the Qt Code of Conduct -

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

  • 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
    public slots:
        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.

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

  • @JohanSolo
    Nothing in MyClass's destructor.


    But in, ~MyClass is called its destructor.

        delete ui;

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


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

    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.

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

  • @HYKim said:

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

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

  • @JohanSolo said:

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


    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:

Log in to reply