Question on memory leak
-
Dear all
I was using visual leak detector for finding memory leaks in my Qt code. VLD prints out many times the following:
@---------- Block 273 at 0x02E60DF0: 8 bytes ----------
Call Stack:
c:\users\gerda\documents\dds_software_projects\concept_discovery_environment_basic\dataset.cpp (86): Concept_Discovery_Environment_Basic.exe!Dataset::setAttributeNames + 0x7 bytes
c:\users\gerda\documents\dds_software_projects\concept_discovery_environment_basic\contextcomposer.cpp (249): Concept_Discovery_Environment_Basic.exe!ContextComposer::loadContextFile
c:\users\gerda\documents\dds_software_builds\debug_concept_discovery_basic_32\debug\moc_contextcomposer.cpp (142): Concept_Discovery_Environment_Basic.exe!ContextComposer::qt_static_metacall + 0x8 bytes
0x5BF961E0 (File and line number not available): Qt5Cored.dll!QTimer::isSingleShot + 0x2ED69E bytes
0x5BF95C11 (File and line number not available): Qt5Cored.dll!QTimer::isSingleShot + 0x2ED0CF bytes
0x5CA7846B (File and line number not available): Qt5Widgetsd.dll!QGraphicsSceneMouseEvent::QGraphicsSceneMouseEvent + 0x192F4 bytes
0x5CA77B47 (File and line number not available): Qt5Widgetsd.dll!QGraphicsSceneMouseEvent::QGraphicsSceneMouseEvent + 0x189D0 bytes
0x5CA6F251 (File and line number not available): Qt5Widgetsd.dll!QGraphicsSceneMouseEvent::QGraphicsSceneMouseEvent + 0x100DA bytes
0x5CCD4208 (File and line number not available): Qt5Widgetsd.dll!QGraphicsSceneMouseEvent::QGraphicsSceneMouseEvent + 0x275091 bytes
0x5CBD6E62 (File and line number not available): Qt5Widgetsd.dll!QGraphicsSceneMouseEvent::QGraphicsSceneMouseEvent + 0x177CEB bytes
0x5CBD7F22 (File and line number not available): Qt5Widgetsd.dll!QGraphicsSceneMouseEvent::QGraphicsSceneMouseEvent + 0x178DAB bytes
0x5CCD32B0 (File and line number not available): Qt5Widgetsd.dll!QGraphicsSceneMouseEvent::QGraphicsSceneMouseEvent + 0x274139 bytes
0x5CADA055 (File and line number not available): Qt5Widgetsd.dll!QGraphicsSceneMouseEvent::QGraphicsSceneMouseEvent + 0x7AEDE bytes
0x5CBD7D41 (File and line number not available): Qt5Widgetsd.dll!QGraphicsSceneMouseEvent::QGraphicsSceneMouseEvent + 0x178BCA bytes
0x5CCD426B (File and line number not available): Qt5Widgetsd.dll!QGraphicsSceneMouseEvent::QGraphicsSceneMouseEvent + 0x2750F4 bytes
0x5CA928CE (File and line number not available): Qt5Widgetsd.dll!QGraphicsSceneMouseEvent::QGraphicsSceneMouseEvent + 0x33757 bytes
0x5CA9093C (File and line number not available): Qt5Widgetsd.dll!QGraphicsSceneMouseEvent::QGraphicsSceneMouseEvent + 0x317C5 bytes
0x5BF474C4 (File and line number not available): Qt5Cored.dll!QTimer::isSingleShot + 0x29E982 bytes
0x5C057198 (File and line number not available): Qt5Cored.dll!QTimer::isSingleShot + 0x3AE656 bytes
0x5CA8FA65 (File and line number not available):
q:\qt5_workdir\w\s\qtbase\src\winmain\qtmain_win.cpp (131): Concept_Discovery_Environment_Basic.exe!WinMain + 0x12 bytes
f:\dd\vctools\crt_bld\self_x86\crt\src\crtexe.c (547): Concept_Discovery_Environment_Basic.exe!__tmainCRTStartup + 0x2C bytes
f:\dd\vctools\crt_bld\self_x86\crt\src\crtexe.c (371): Concept_Discovery_Environment_Basic.exe!WinMainCRTStartup
0x767933AA (File and line number not available): kernel32.dll!BaseThreadInitThunk + 0x12 bytes
0x77209EF2 (File and line number not available): ntdll.dll!RtlInitializeExceptionChain + 0x63 bytes
0x77209EC5 (File and line number not available): ntdll.dll!RtlInitializeExceptionChain + 0x36 bytes
Data:
00 8A 17 01 B0 AD 19 03 ........ ........@
I had to remove something in the middle because of the 6000 characters limit.
The corresponding code is the following:
@for (int i = 0; i < attributeNames->count(); i++)
{
QStandardItem *temp = new QStandardItem(attributeNames->at(i)); //<--- mem leak here?
temp->setCheckable(true);
temp->setCheckState(Qt::Checked);
attributeTreeModel->appendRow(temp);
}@Is there indeed a memory leak?
Hatchi
-
I don't know VLD but where in the output do you suggest is a warning about a leak?
-
Dear sir
The code which i posted was executed about 50 times and in each iteration VLD suggests there is a memory leak at line 86 (I marked it with a comment). According to VLD on each iteration, 8 bytes are lost.
This confuses me since I thought the QStandardItemModel takes over ownership and takes care of deletion...
-
I think the memory leak depends more on the attributeTreeModel than on the line you marked. Does attributeTreeModel automatically delete the QStandardItem objects or only gets rid of the pointers when deleted? If not, you must delete them manually.
-
8 Bytes seems to little for a full QStandardItem. It's the size of just the pointer to it (or to anything else) on a 64 bit system. Looks like a false positive to me, to be honest. I assume attributeTreeModel is a normal abstract item model which properly takes ownership of the items? Did you try checking the code with valgrind?