Re: memory leak in a MFC application
On Mar 26, 12:16 pm, Joseph M. Newcomer <newco...@flounder.com> wrote:
Your evaluation suggests that DoNeedle is the source of the problem. I=
f you can do
something that produces the *same* values, {898} for example, then you ca=
n find the source
of the leak fairly easily.
Use the debugger to set the variable _crtBreakAlloc to the number you see=
(898 in the
example below) and you will get a debug break when that is allocated. =
Track back in the
stack to see who is allocating it. and you will know what allocation is n=
ot being freed.
Note that a lot of the files generate by Microsoft include the declaratio=
n
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
If you are writing files that were not generated by the wizards, add thos=
e lines at the
front, and you will get the filename/line info in your debug report.
=
joe
On Wed, 25 Mar 2009 15:42:04 -0700 (PDT), ssylee <staniga...@gmail.com> w=
rote:
I have a single-document view MFC application as attached to this link
(https://www.yousendit.com/download/UmNMZGVkNEhkMnV4dnc9PQ). This
application would need to link with OpenGL libraries and dlls in order
to run. In addition, Intel Math Kernel Library (or Intel Compiler)
needs to be installed on the system in order for the application to
run.
My memory leak problem, at least I found, has something to do with
this snippet in CsplitterView::OnTimer handler:
if(startsim && needlemodel)
{
//theMesh->NodesRepoint(needlemo=
del->Xall);
theMesh->nodesdeleteignore = t=
rue; // change this parameter's
scope back to private
startsim = false;
}
if (theMesh->nodesdeleteignore &&
needlemodel)
{
if (count < needlemodel->needleB=
asePos.entries)
eedle(&(needlemodel->needleBasePos.nodepts
[6*count]));
count++;
}
}
If "needlemodel->DoNeedle(&(needlemodel->needleBasePos.nodepts
[6*count]));" gets commented out, I don't see any memory leak problem
showing up in the debug window. If that function is called on a
consistent basis as the "Start" button in the application is pressed,
as I close the window, I see a group of memory leak messages from
Visual Studio debug output as indicated:
Detected memory leaks!
Dumping objects ->
{898} normal block at 0x04415428, 480128 bytes long.
Data: < > CD CD CD CD CD CD CD CD CD CD =
CD CD CD CD CD
CD
{897} normal block at 0x02E4F8C0, 13184 bytes long.
Data: < > CD CD CD CD CD CD CD CD CD CD =
CD CD CD CD CD
CD
{896} normal block at 0x02E4C380, 120 bytes long.
Data: < (TA > C0 F8 E4 02 28 54 41 04 00 00 00 0=
0 00 00 00
00
{895} normal block at 0x02E4C500, 13184 bytes long.
Data: < > CD CD CD CD CD CD CD CD CD CD =
CD CD CD CD CD
CD
{894} normal block at 0x046A0040, 935040 bytes long.
Data: < > CD CD CD CD CD CD CD CD CD CD =
CD CD CD CD CD
CD
{889} normal block at 0x043A0068, 480128 bytes long.
Data: < > CD CD CD CD CD CD CD CD CD CD =
CD CD CD CD CD
CD
{888} normal block at 0x02E4C448, 120 bytes long.
Data: <h : @ j > 68 00 3A 04 40 00 6A 04 00 C5 E4 02 00=
00 00
00
Object dump complete.
I have used the memory profiler as well as data debug point in Visual
Studio debugger to check when the data in any one of the addresses as
indicated in the previous message change. However, I have found them
to be changing and allocated in Microsoft-implemented function calls
that I have no idea of. I suspect I'm using the MFC API improperly
somewhere in the project, but I have no idea what. After doing some
search on the internet, I have tried setting a breakpoint on a memory
allocation number (http://msdn.microsoft.com/en-us/library/w2fhc9a3(VS.
80).aspx), but have not much luck either.
When I ran the same function in a separate console project, I don't
see the same errors in the debug output window. You can try running it
for yourselves as I have attached the project to this link (https://
www.yousendit.com/download/UmNMZGVYQzNEa1ZjR0E9PQ). Again, Intel Math
Kernel Library needs to be installed before this application can be
run properly.
Sorry for the long email. Would really appreciate the explanation of
what's going on.
Joseph M. Newcomer [MVP]
email: newco...@flounder.com
Web:http://www.flounder.com
MVP Tips:http://www.flounder.com/mvp_tips.htm
Thanks for the videos and links in terms of memory leak finding
tutorials. I have added the MFC CMemoryState object instantiation and
method calls in the following manner in splitterView.cpp where
DoNeedle is being called:
if(startsim && needlemodel)
{
//theMesh->NodesRepoint(needlemodel->Xall);
theMesh->nodesdeleteignore = true; // change this parameter's
scope back to private
startsim = false;
}
if (theMesh->nodesdeleteignore &&
needlemodel)
{
if (count < needlemodel->needleBasePos.entries)
{
#if _DEBUG
CMemoryState oldstate, newstate, diffstate;
oldstate.Checkpoint();
#endif
// Note: memory leak indeed comes from this function
// need to determine the meaning of the memory leak diagnostics
needlemodel->DoNeedle(&(needlemodel->needleBasePos.nodepts
[6*count]));
count++;
#if _DEBUG
newstate.Checkpoint();
if(diffstate.Difference(oldstate, newstate))
{
AfxMessageBox("Memory Leak Detected");
diffstate.DumpAllObjectsSince();
}
#endif
}
}
The message box does pop up on certain calls to DoNeedle function.
However, as I step through the code inside DoNeedle in the debugger on
the cases where the memory state has changed, I can't find any new
memory allocations (at least the one that I have made anyway). I am
extremely certain that all the malloc/free pairs in the DoNeedle
function are paired properly in the Needle3D class from my search in
that project. There are no new/delete pairs in the Needle3D class. I
have no knowledge in terms of context of the additional new operator
calls that are called. Below is the printout of the
DumpAllObjectsSince function during a particular instance.
Dumping objects ->
{661} normal block at 0x119B58C8, 480128 bytes long.
Data: < > CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD
CD
{660} normal block at 0x02E896E0, 13184 bytes long.
Data: < > CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD
CD
{659} normal block at 0x02E860D8, 120 bytes long.
Data: < X > E0 96 E8 02 C8 58 9B 11 00 00 00 00 00 00 00
00
{658} normal block at 0x02E86320, 13184 bytes long.
Data: < > CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD
CD
{657} normal block at 0x11E50040, 935040 bytes long.
Data: < > CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD
CD
{652} normal block at 0x11940068, 480128 bytes long.
Data: < > CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD
CD
{651} normal block at 0x02E85F78, 120 bytes long.
Data: <h @ c > 68 00 94 11 40 00 E5 11 20 63 E8 02 00 00 00
00
f:\rtm\vctools\vc7libs\ship\atlmfc\src\mfc\strcore.cpp(141) : {236}
normal block at 0x02E862C8, 22 bytes long.
Data: < >x > EC 97 3E 78 05 00 00 00 05 00 00 00 01 00 00
00
f:\rtm\vctools\vc7libs\ship\atlmfc\src\mfc\strcore.cpp(141) : {222}
normal block at 0x00369320, 25 bytes long.
Data: < >x > EC 97 3E 78 08 00 00 00 08 00 00 00 01 00 00
00
f:\rtm\vctools\vc7libs\ship\atlmfc\src\mfc\strcore.cpp(173) : {218}
normal block at 0x02E85CC0, 56 bytes long.
Data: < >x' ' > EC 97 3E 78 27 00 00 00 27 00 00 00 01 00 00
00
f:\rtm\vctools\vc7libs\ship\atlmfc\src\mfc\array_p.cpp(110) : {217}
normal block at 0x02E85C70, 20 bytes long.
Data: < 6 > 00 00 00 00 D4 91 36 00 00 00 00 00 CD CD CD
CD
f:\rtm\vctools\vc7libs\ship\atlmfc\src\mfc\array_p.cpp(67) : {216}
normal block at 0x02E85C30, 4 bytes long.
Data: < > 00 00 00 00
f:\rtm\vctools\vc7libs\ship\atlmfc\src\mfc\winfrm2.cpp(59) : {215}
client block at 0x02E85B30, subtype c0, 192 bytes long.
a CDockBar object at $02E85B30, 192 bytes long
f:\rtm\vctools\vc7libs\ship\atlmfc\src\mfc\array_p.cpp(67) : {213}
normal block at 0x02E85A38, 4 bytes long.
Data: < > 00 00 00 00
f:\rtm\vctools\vc7libs\ship\atlmfc\src\mfc\winfrm2.cpp(59) : {212}
client block at 0x02E85938, subtype c0, 192 bytes long.
a CDockBar object at $02E85938, 192 bytes long
f:\rtm\vctools\vc7libs\ship\atlmfc\src\mfc\array_p.cpp(67) : {211}
normal block at 0x02E858F8, 4 bytes long.
Data: < > 00 00 00 00
f:\rtm\vctools\vc7libs\ship\atlmfc\src\mfc\winfrm2.cpp(59) : {210}
client block at 0x02E857F8, subtype c0, 192 bytes long.
a CDockBar object at $02E857F8, 192 bytes long
f:\rtm\vctools\vc7libs\ship\atlmfc\src\mfc\winfrm2.cpp(59) : {208}
client block at 0x02E856B8, subtype c0, 192 bytes long.
a CDockBar object at $02E856B8, 192 bytes long
f:\rtm\vctools\vc7libs\ship\atlmfc\src\mfc\bardock.cpp(732) : {207}
normal block at 0x02E855C8, 176 bytes long.
Data: < x > B0 8F 1D 78 CD CD CD CD CD CD CD CD CD CD CD
CD
f:\rtm\vctools\vc7libs\ship\atlmfc\src\mfc\strcore.cpp(141) : {205}
normal block at 0x02E85570, 21 bytes long.
Data: < >x > EC 97 3E 78 04 00 00 00 04 00 00 00 01 00 00
00
f:\rtm\vctools\vc7libs\ship\atlmfc\src\mfc\strcore.cpp(141) : {204}
normal block at 0x02DCD448, 20 bytes long.
Data: < >x > EC 97 3E 78 03 00 00 00 03 00 00 00 01 00 00
00
f:\rtm\vctools\vc7libs\ship\atlmfc\src\mfc\strcore.cpp(141) : {203}
normal block at 0x02DCD3F8, 20 bytes long.
Data: < >x > EC 97 3E 78 03 00 00 00 03 00 00 00 01 00 00
00
f:\rtm\vctools\vc7libs\ship\atlmfc\src\mfc\barcore.cpp(156) : {202}
normal block at 0x0036AAE0, 80 bytes long.
Data: < h > 00 00 00 00 68 01 00 00 00 01 00 08 00 00 00
00
f:\rtm\vctools\vc7libs\ship\atlmfc\src\mfc\plex.cpp(29) : {199} normal
block at 0x02E854B8, 124 bytes long.
Data: < T 6 > 00 00 00 00 C8 54 E8 02 00 00 00 00 D4 91 36
00
{198} client block at 0x02DCD360, subtype c0, 88 bytes long.
a usoundView object at $02DCD360, 88 bytes long
c:\robotics and controls laboratory\splitter\splitterview.cpp(382) :
{188} client block at 0x02DCD1D8, subtype c0, 20 bytes long.
a CClientDC object at $02DCD1D8, 20 bytes long
{187} normal block at 0x02E851C0, 696 bytes long.
Data: < e > 90 09 00 00 91 09 00 00 92 09 00 00 65 0D 00
00
{186} normal block at 0x02E84DE0, 928 bytes long.
Data: <0 w } > 30 03 00 00 77 04 00 00 7D 03 00 00 BB 00 00
00
{185} normal block at 0x02E83F20, 3712 bytes long.
Data: < > 00 00 00 00 01 00 00 00 02 00 00 00 03 00 00
00
{184} normal block at 0x02E83060, 3712 bytes long.
Data: < > 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00
{183} normal block at 0x02E82C80, 928 bytes long.
Data: <0 w } > 30 03 00 00 77 04 00 00 7D 03 00 00 BB 00 00
00
{182} normal block at 0x10320040, 11592576 bytes long.
Data: < > 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00
{181} normal block at 0x0FAF0040, 1937664 bytes long.
Data: < > 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00
{180} normal block at 0x0F910040, 1937664 bytes long.
Data: <v 7; C0 > 76 B7 37 3B C8 D2 43 30 18 15 AE B0 00 00 00
00
{179} normal block at 0x0F730040, 1937664 bytes long.
Data: < ?: +4a W > 03 00 80 3F 3A 9A 2B 34 61 07 57 B4 00 00 00
00
{178} normal block at 0x0F550040, 1937664 bytes long.
Data: < 7;L } e/ > BA B7 37 3B 4C 91 BA AE 7D D1 65 2F 00 00 00
00
{177} normal block at 0x02DCCDF8, 928 bytes long.
Data: < > CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD
CD
{176} normal block at 0x02DCC458, 2400 bytes long.
Data: < > 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00
{175} normal block at 0x02E7EB30, 16656 bytes long.
Data: <y; ; 4 3 = ;> 79 3B A5 3B 91 8D 34 BC 33 14 CC 3D 9D 96 A7
3B
{174} normal block at 0x0036A7D8, 120 bytes long.
Data: < =V94> t = => E7 FB A9 3D 56 39 34 3E BC 74 93 3D E7 FB A=
9
3D
{173} normal block at 0x0036A720, 120 bytes long.
Data: < > 00 00 00 00 0A D7 A3 BC 00 00 00 00 00 00 00
00
{172} normal block at 0x02E7D540, 5552 bytes long.
Data: < > 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00
{171} normal block at 0x02E7BF50, 5552 bytes long.
Data: < > 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00
{170} normal block at 0x02E77E00, 16656 bytes long.
Data: < =X < t = > E7 FB A9 3D 58 12 83 3C BC 74 93 3D 00 00 00
00
{169} normal block at 0x02E6BA90, 49968 bytes long.
Data: < ? > 00 00 80 3F 00 00 00 00 00 00 00 00 00 00 00
00
{168} normal block at 0x02E5F720, 49968 bytes long.
Data: < ? > 00 00 80 3F 00 00 00 80 00 00 00 00 00 00 00
00
{167} normal block at 0x02E5E130, 5552 bytes long.
Data: < L> > CD CC 4C 3E 00 00 00 00 00 00 00 00 00 00 00
00
{166} normal block at 0x02E5CB40, 5552 bytes long.
Data: < > 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00
{165} normal block at 0x0036A6E0, 4 bytes long.
Data: < > 01 00 00 00
{164} normal block at 0x02E5B550, 5552 bytes long.
Data: <0 > 30 03 00 00 00 00 00 00 00 00 00 00 00 00 00
00
{163} normal block at 0x0036FFE8, 4 bytes long.
Data: < > FF FF FF FF
{162} normal block at 0x0036A628, 120 bytes long.
Data: < > 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00
{161} normal block at 0x02E57400, 16656 bytes long.
Data: < @ > FF FF BF 40 00 00 00 00 00 00 00 00 00 00 00
00
{160} normal block at 0x02E532B0, 16656 bytes long.
Data: < ?1 \9 > 8C C9 FA 3F 31 E4 AE C0 5C 39 A7 BF 00 00 00
00
{159} normal block at 0x02E4F160, 16656 bytes long.
Data: < > 00 00 00 00 C6 12 83 BB 00 00 00 00 00 00 00
00
{158} normal block at 0x02E4B010, 16656 bytes long.
Data: < > 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00
{157} normal block at 0x02E46EC0, 16656 bytes long.
Data: < = t = > E7 FB A9 3D C6 12 83 BB BC 74 93 3D 00 00 00
00
{156} normal block at 0x0B320040, 69355584 bytes long.
Data: <eF#E/ . 0 > 65 46 23 45 2F E8 F4 C3 2E 19 D3 C3 30 E8 F4
C3
{155} normal block at 0x070F0040, 69355584 bytes long.
Data: <:f 9 2 9{^O8 > 3A 66 DA 39 13 32 10 39 7B 5E 4F 38 00 00 00
00
{154} normal block at 0x0036A5E0, 12 bytes long.
Data: < = #< t => E7 FB A9 3D 00 D7 23 3C BC 74 93 3D
{153} normal block at 0x0036BC10, 24 bytes long.
Data: < 1 @\9 ?} > 8C C9 FA BF 31 E4 AE 40 5C 39 A7 3F 7D C7 85
BE
{152} normal block at 0x02E3AFB0, 48848 bytes long.
Data: < > CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD
CD
{151} normal block at 0x02E38410, 11104 bytes long.
Data: < @ HC @ HC> 00 00 C0 40 00 00 48 43 00 00 C0 40 00 00 48
43
{150} normal block at 0x0036FE18, 404 bytes long.
Data: < > 12 02 00 00 13 02 00 00 14 02 00 00 15 02 00
00
{149} normal block at 0x02E37DE8, 1516 bytes long.
Data: < > 00 00 00 00 01 00 00 00 02 00 00 00 03 00 00
00
{148} normal block at 0x02DE93C8, 322016 bytes long.
Data: < 5 t > 80 02 00 00 35 07 00 00 0C 08 00 00 74 09 00
00
{147} normal block at 0x02EC0040, 69355584 bytes long.
Data: < ? > 00 00 80 3F 00 00 00 00 00 00 00 00 00 00 00
00
{146} normal block at 0x02DE5278, 16656 bytes long.
Data: <y; ; 4 3 = ;> 79 3B A5 3B 91 8D 34 BC 33 14 CC 3D 9D 96 A7
3B
{145} normal block at 0x02DCD498, 97696 bytes long.
Data: < > BA 00 00 00 0F 02 00 00 0E 02 00 00 B5 00 00
00
{143} normal block at 0x00369C40, 2400 bytes long.
Data: < == W> t = > E7 FB A9 3D 3D 0A 57 3E BC 74 93 3D 00 00 0=
0
00
c:\robotics and controls laboratory\splitter\splitterview.cpp(59) :
{140} normal block at 0x00369B30, 208 bytes long.
Data: < D : { > DA 17 00 00 44 10 00 00 3A 00 00 00 7B 01 00
00
{139} normal block at 0x02DC8308, 16656 bytes long.
Data: < ? > 00 00 00 00 00 00 80 3F 00 00 00 00 00 00 00
00
{138} normal block at 0x02DC41B8, 16656 bytes long.
Data: <y; ; 4 3 = ;> 79 3B A5 3B 91 8D 34 BC 33 14 CC 3D 9D 96 A7
3B
{137} normal block at 0x02DC0068, 16656 bytes long.
Data: <y; ; 4 3 = ;> 79 3B A5 3B 91 8D 34 BC 33 14 CC 3D 9D 96 A7
3B
{136} normal block at 0x0036BC80, 12570 bytes long.
Data: < 2 + > 02 00 00 00 06 00 02 00 32 00 00 00 2B 00 06
00
{134} normal block at 0x0036AB70, 4190 bytes long.
Data: < > 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00
c:\robotics and controls laboratory\splitter\splitterview.cpp(57) :
{131} normal block at 0x00369A70, 40 bytes long.
Data: <d Z / l > 64 BE 5A 00 04 00 00 00 2F 08 00 00 6C 05 00
00
c:\robotics and controls laboratory\splitter\splitterview.cpp(21) :
{130} client block at 0x003699A0, subtype c0, 144 bytes long.
a CsplitterView object at $003699A0, 144 bytes long
{129} normal block at 0x00369958, 12 bytes long.
Data: < > 00 00 00 00 09 02 00 00 F5 00 00 00
{128} normal block at 0x00369838, 24 bytes long.
Data: < > 00 00 00 00 0A 02 00 00 0A 02 00 00 00 00 00
00
f:\rtm\vctools\vc7libs\ship\atlmfc\src\mfc\plex.cpp(29) : {124} normal
block at 0x003698A0, 124 bytes long.
Data: < 6 6 > 00 00 00 00 B0 98 36 00 00 00 00 00 C8 96 36
00
{120} client block at 0x003696C8, subtype c0, 152 bytes long.
a NeedleInsertControl object at $003696C8, 152 bytes long
{119} normal block at 0x00369670, 24 bytes long.
Data: < > 00 00 00 00 F5 00 00 00 F5 00 00 00 00 00 00
00
{118} normal block at 0x00369628, 12 bytes long.
Data: < 0 , > 00 00 00 00 30 04 00 00 2C 04 00 00
c:\robotics and controls laboratory\splitter\mainfrm.cpp(19) : {110}
client block at 0x00368EB0, subtype c0, 988 bytes long.
a CMainFrame object at $00368EB0, 988 bytes long
c:\robotics and controls laboratory\splitter\splitterdoc.cpp(16) :
{109} client block at 0x00368E20, subtype c0, 84 bytes long.
a CsplitterDoc object at $00368E20, 84 bytes long
f:\rtm\vctools\vc7libs\ship\atlmfc\src\mfc\plex.cpp(29) : {108} normal
block at 0x00368D68, 124 bytes long.
Data: < 6 > 00 00 00 00 00 00 00 00 00 00 00 00 D8 8B 36
00
{107} client block at 0x00368D08, subtype c0, 32 bytes long.
a CDocManager object at $00368D08, 32 bytes long
f:\rtm\vctools\vc7libs\ship\atlmfc\src\mfc\strcore.cpp(141) : {106}
normal block at 0x00368C80, 73 bytes long.
Data: < >x8 8 > EC 97 3E 78 38 00 00 00 38 00 00 00 01 00 00
00
c:\robotics and controls laboratory\splitter\splitter.cpp(75) : {105}
client block at 0x00368BD8, subtype c0, 108 bytes long.
a CSingleDocTemplate object at $00368BD8, 108 bytes long
f:\rtm\vctools\vc7libs\ship\atlmfc\src\mfc\strcore.cpp(141) : {103}
normal block at 0x00368B80, 23 bytes long.
Data: < >x > EC 97 3E 78 06 00 00 00 06 00 00 00 01 00 00
00
f:\rtm\vctools\vc7libs\ship\atlmfc\src\mfc\strcore.cpp(141) : {102}
normal block at 0x00368B20, 33 bytes long.
Data: < >x > EC 97 3E 78 10 00 00 00 10 00 00 00 01 00 00
00
{101} normal block at 0x00368AD0, 20 bytes long.
Data: < >x >x >x> 04 00 00 00 00 98 3E 78 00 98 3E 78 00 98 3E
78
{100} normal block at 0x00368A70, 32 bytes long.
Data: <$R x 6 0 6 > 24 52 1E 78 04 00 00 00 D4 8A 36 00 30 8B 36
00
f:\rtm\vctools\vc7libs\ship\atlmfc\src\mfc\oleinit.cpp(84) : {97}
client block at 0x003689F0, subtype c0, 68 bytes long.
a CCmdTarget object at $003689F0, 68 bytes long
{87} client block at 0x003641B8, subtype c0, 64 bytes long.
a CDynLinkLibrary object at $003641B8, 64 bytes long
Object dump complete.
I believe the objects that are dumped are the diffstate's ones. Is it
safe to assume that only objects seen in the newState but not oldState
is being dumped?