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 { 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.
-
@JohanSolo
Nothing in MyClass's destructor.MyClass::~MyClass(){ }
But in MainWindow.cc, ~MyClass is called its destructor.
MainWindow::~MainWindow() { myClass->~MyClass(); delete ui; }
-
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 youMyClass
instance, but the allocated memory is never freed, which then may trigger the error when theMainWindow
destructor trips on themyClass
pointer. -
@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