Discussion:
Properly adding CString struction to CList pointer
(too old to reply)
Jaime Rios
2004-10-04 16:43:05 UTC
Permalink
Hi,
I have a member variable for my class that is declared:
CList <PARAMITEMS, PARAMITEMS&>m_listParamItems;

and it contains a structure with two CString variables, declared as:

struct PARAMITEMS
{
CString strParamName;
CString strParam;
};

I have a memory leak in my application that is driving me nuts and the only
place that I can locate it, is at this section of code:

PARAMITEMS* pParam = new PARAMITEMS;

pParam->strParamName = (const char*)"FIRST_PARAMETER";
pParam->strParam = strCString; // Test CString allocated with "String"

m_listParamItems.AddTail(*pParam);
delete pParam;

After my function is done, I call the function _CrtDumpMemoryLeaks() and I
get the following:
Detected memory leaks!
Dumping objects ->
{62} normal block at 0x09780FF0, 4 bytes long.
Data: <c:\ > 63 3A 5C 00
{61} normal block at 0x0977EFE0, 16 bytes long.
Data: <FIRST_PARAMETER > 46 49 52 53 54 5F 50 41 52 41 4D 45 54 45 52 00
{60} normal block at 0x0977CFE0, 16 bytes long.
Data: < w ( o > 01 00 00 00 E0 EF 77 09 FF FF FF FF 28 06 6F 09
{59} normal block at 0x0977AFE0, 24 bytes long.
Data: < ow > 00 00 00 00 00 00 00 00 02 00 00 00 F0 6F 77 09
{58} normal block at 0x09778FE0, 19 bytes long.
Data: <EXCLUDED_STATEME> 45 58 43 4C 55 44 45 44 5F 53 54 41 54 45 4D 45
{57} normal block at 0x09776FF0, 6 bytes long.
Data: </file > 2F 66 69 6C 65 00
{56} normal block at 0x09774FE0, 16 bytes long.
Data: < ow w > 02 00 00 00 F0 6F 77 09 02 00 00 00 E0 8F 77 09
{55} normal block at 0x09772FF0, 12 bytes long.
Data: <c:\ /file > 63 3A 5C 20 2F 66 69 6C 65 00 03 00
{54} client block at 0x09770F20, subtype 0, 216 bytes long.
a CFileFind object at $09770F20, 216 bytes long
dllmodul.cpp(106) : {53} client block at 0x0976EFB0, subtype 0, 64 bytes long.
a CDynLinkLibrary object at $0976EFB0, 64 bytes long
{52} client block at 0x0976CFB0, subtype 0, 64 bytes long.
a CDynLinkLibrary object at $0976CFB0, 64 bytes long
{50} client block at 0x09768FB0, subtype 0, 64 bytes long.
a CDynLinkLibrary object at $09768FB0, 64 bytes long
Object dump complete.

What am I doing wrong with the CList allocation? Thanks in advance!
Sigurd Stenersen
2004-10-04 17:10:05 UTC
Permalink
Post by Jaime Rios
struct PARAMITEMS
{
CString strParamName;
CString strParam;
};
CList <PARAMITEMS, PARAMITEMS &> m_listParamItems;
(I switched the order of these two)
Post by Jaime Rios
PARAMITEMS* pParam = new PARAMITEMS;
pParam->strParamName = (const char*)"FIRST_PARAMETER";
pParam->strParam = strCString; // Test CString allocated with "String"
m_listParamItems.AddTail(*pParam);
delete pParam;
After my function is done, I call the function _CrtDumpMemoryLeaks()
Detected memory leaks!
This code does not make any leaks on my system. Are you calling
_CrtDumpMemoryLeaks() before m_listParamItems is destroyed ?

And why are you calling _CrtDumpMemoryLeaks() in the first place ? It's
supposed to be done automatically in MFC debug builds...
--
Sigurd
http://utvikling.com
Jaime Rios
2004-10-04 17:53:05 UTC
Permalink
Hi,
I removed the _CrtDumpMemoryLeaks() throughout my entire project and now
when I look at the debugging window, I see this after the call to delete:

Detected memory leaks!
Dumping objects ->
c:\program files\microsoft visual studio\vc98\include\crtdbg.h(552) : {59}
normal block at 0x0977AFE0, 24 bytes long.
Data: < ow > 00 00 00 00 00 00 00 00 02 00 00 00 F0 6F 77 09
c:\program files\microsoft visual studio\vc98\include\crtdbg.h(552) : {58}
normal block at 0x09778FE0, 19 bytes long.
Data: <EXCLUDED_STATEME> 45 58 43 4C 55 44 45 44 5F 53 54 41 54 45 4D 45
c:\program files\microsoft visual studio\vc98\include\crtdbg.h(552) : {57}
normal block at 0x09776FF0, 6 bytes long.
Data: </file > 2F 66 69 6C 65 00
c:\program files\microsoft visual studio\vc98\include\crtdbg.h(552) : {56}
normal block at 0x09774FE0, 16 bytes long.
Data: < ow w > 02 00 00 00 F0 6F 77 09 01 00 00 00 E0 8F 77 09
c:\program files\microsoft visual studio\vc98\include\crtdbg.h(552) : {55}
normal block at 0x09772FF0, 12 bytes long.
Data: <c:\ /file > 63 3A 5C 20 2F 66 69 6C 65 00 03 00
{54} client block at 0x09770F20, subtype 0, 216 bytes long.
a CFileFind object at $09770F20, 216 bytes long
dllmodul.cpp(106) : {53} client block at 0x0976EFB0, subtype 0, 64 bytes long.
a CDynLinkLibrary object at $0976EFB0, 64 bytes long
{52} client block at 0x0976CFB0, subtype 0, 64 bytes long.
a CDynLinkLibrary object at $0976CFB0, 64 bytes long
{50} client block at 0x09768FB0, subtype 0, 64 bytes long.
a CDynLinkLibrary object at $09768FB0, 64 bytes long
Object dump complete.

Where am I going wrong? Thanks.
Post by Sigurd Stenersen
Post by Jaime Rios
struct PARAMITEMS
{
CString strParamName;
CString strParam;
};
CList <PARAMITEMS, PARAMITEMS &> m_listParamItems;
(I switched the order of these two)
Post by Jaime Rios
PARAMITEMS* pParam = new PARAMITEMS;
pParam->strParamName = (const char*)"FIRST_PARAMETER";
pParam->strParam = strCString; // Test CString allocated with "String"
m_listParamItems.AddTail(*pParam);
delete pParam;
After my function is done, I call the function _CrtDumpMemoryLeaks()
Detected memory leaks!
This code does not make any leaks on my system. Are you calling
_CrtDumpMemoryLeaks() before m_listParamItems is destroyed ?
And why are you calling _CrtDumpMemoryLeaks() in the first place ? It's
supposed to be done automatically in MFC debug builds...
--
Sigurd
http://utvikling.com
Jaime Rios
2004-10-04 17:59:02 UTC
Permalink
Hi,
When I click on the first line, this is where the debug window brings me...
#ifdef _CRTDBG_MAP_ALLOC

inline void* __cdecl operator new(unsigned int s)
{ return ::operator new(s, _NORMAL_BLOCK, __FILE__, __LINE__); }

#endif /* _CRTDBG_MAP_ALLOC */

Still don't know why I am having problems. Thanks.
Post by Jaime Rios
Hi,
I removed the _CrtDumpMemoryLeaks() throughout my entire project and now
Detected memory leaks!
Dumping objects ->
c:\program files\microsoft visual studio\vc98\include\crtdbg.h(552) : {59}
normal block at 0x0977AFE0, 24 bytes long.
Data: < ow > 00 00 00 00 00 00 00 00 02 00 00 00 F0 6F 77 09
c:\program files\microsoft visual studio\vc98\include\crtdbg.h(552) : {58}
normal block at 0x09778FE0, 19 bytes long.
Data: <EXCLUDED_STATEME> 45 58 43 4C 55 44 45 44 5F 53 54 41 54 45 4D 45
c:\program files\microsoft visual studio\vc98\include\crtdbg.h(552) : {57}
normal block at 0x09776FF0, 6 bytes long.
Data: </file > 2F 66 69 6C 65 00
c:\program files\microsoft visual studio\vc98\include\crtdbg.h(552) : {56}
normal block at 0x09774FE0, 16 bytes long.
Data: < ow w > 02 00 00 00 F0 6F 77 09 01 00 00 00 E0 8F 77 09
c:\program files\microsoft visual studio\vc98\include\crtdbg.h(552) : {55}
normal block at 0x09772FF0, 12 bytes long.
Data: <c:\ /file > 63 3A 5C 20 2F 66 69 6C 65 00 03 00
{54} client block at 0x09770F20, subtype 0, 216 bytes long.
a CFileFind object at $09770F20, 216 bytes long
dllmodul.cpp(106) : {53} client block at 0x0976EFB0, subtype 0, 64 bytes long.
a CDynLinkLibrary object at $0976EFB0, 64 bytes long
{52} client block at 0x0976CFB0, subtype 0, 64 bytes long.
a CDynLinkLibrary object at $0976CFB0, 64 bytes long
{50} client block at 0x09768FB0, subtype 0, 64 bytes long.
a CDynLinkLibrary object at $09768FB0, 64 bytes long
Object dump complete.
Where am I going wrong? Thanks.
Post by Sigurd Stenersen
Post by Jaime Rios
struct PARAMITEMS
{
CString strParamName;
CString strParam;
};
CList <PARAMITEMS, PARAMITEMS &> m_listParamItems;
(I switched the order of these two)
Post by Jaime Rios
PARAMITEMS* pParam = new PARAMITEMS;
pParam->strParamName = (const char*)"FIRST_PARAMETER";
pParam->strParam = strCString; // Test CString allocated with "String"
m_listParamItems.AddTail(*pParam);
delete pParam;
After my function is done, I call the function _CrtDumpMemoryLeaks()
Detected memory leaks!
This code does not make any leaks on my system. Are you calling
_CrtDumpMemoryLeaks() before m_listParamItems is destroyed ?
And why are you calling _CrtDumpMemoryLeaks() in the first place ? It's
supposed to be done automatically in MFC debug builds...
--
Sigurd
http://utvikling.com
Sigurd Stenersen
2004-10-04 19:07:17 UTC
Permalink
Post by Jaime Rios
I removed the _CrtDumpMemoryLeaks() throughout my entire project and now
Detected memory leaks!
Where am I going wrong? Thanks.
Like I said, the code you posted does not cause any memory leaks. Maybe you
want to sit down for a minute and think about this and then post some more
code if you can't figure it out ?
--
Sigurd
http://utvikling.com
Jaime Rios
2004-10-04 19:33:06 UTC
Permalink
I guess I could take this DLL and convert it to a console app. Maybe that
will help me find the problem... :(
Post by Sigurd Stenersen
Post by Jaime Rios
I removed the _CrtDumpMemoryLeaks() throughout my entire project and now
Detected memory leaks!
Where am I going wrong? Thanks.
Like I said, the code you posted does not cause any memory leaks. Maybe you
want to sit down for a minute and think about this and then post some more
code if you can't figure it out ?
--
Sigurd
http://utvikling.com
Doug Harrison [MVP]
2004-10-05 02:55:12 UTC
Permalink
Post by Jaime Rios
Hi,
CList <PARAMITEMS, PARAMITEMS&>m_listParamItems;
struct PARAMITEMS
{
CString strParamName;
CString strParam;
};
I have a memory leak in my application that is driving me nuts and the only
PARAMITEMS* pParam = new PARAMITEMS;
pParam->strParamName = (const char*)"FIRST_PARAMETER";
pParam->strParam = strCString; // Test CString allocated with "String"
m_listParamItems.AddTail(*pParam);
delete pParam;
Although you've matched your new with a delete, what if AddTail or other
intervening code throws an exception? You can make this code exception-safe
by using smart pointers, but there's no reason to allocate pParam
dynamically, unless AddTail does something very silly, like storing the
address of the object rather than a copy of the object, in which case, your
code is sunk anyway. Just say:

PARAMITEMS param;
param.strParamName = "FIRST_PARAMETER"; // NB: Deleted pointless cast
param.strParam = strCString; // Test CString allocated with "String"
m_listParamItems.AddTail(param);
Post by Jaime Rios
After my function is done, I call the function _CrtDumpMemoryLeaks() and I
It pretty much never makes sense to call _CrtDumpMemoryLeaks yourself, since
that function dumps memory allocations since the program began to run. The
only proper place to call it is after the program has finished destroying
static duration objects, and the only thing that can do that is the CRT
itself. You might find other _Crt functions more useful, as they allow you
to set up "checkpoints" and determined what happened between two
checkpoints.
Post by Jaime Rios
Detected memory leaks!
Dumping objects ->
{62} normal block at 0x09780FF0, 4 bytes long.
Data: <c:\ > 63 3A 5C 00
{61} normal block at 0x0977EFE0, 16 bytes long.
Data: <FIRST_PARAMETER > 46 49 52 53 54 5F 50 41 52 41 4D 45 54 45 52 00
{60} normal block at 0x0977CFE0, 16 bytes long.
Data: < w ( o > 01 00 00 00 E0 EF 77 09 FF FF FF FF 28 06 6F 09
{59} normal block at 0x0977AFE0, 24 bytes long.
Data: < ow > 00 00 00 00 00 00 00 00 02 00 00 00 F0 6F 77 09
{58} normal block at 0x09778FE0, 19 bytes long.
Data: <EXCLUDED_STATEME> 45 58 43 4C 55 44 45 44 5F 53 54 41 54 45 4D 45
{57} normal block at 0x09776FF0, 6 bytes long.
Data: </file > 2F 66 69 6C 65 00
{56} normal block at 0x09774FE0, 16 bytes long.
Data: < ow w > 02 00 00 00 F0 6F 77 09 02 00 00 00 E0 8F 77 09
{55} normal block at 0x09772FF0, 12 bytes long.
Data: <c:\ /file > 63 3A 5C 20 2F 66 69 6C 65 00 03 00
{54} client block at 0x09770F20, subtype 0, 216 bytes long.
a CFileFind object at $09770F20, 216 bytes long
dllmodul.cpp(106) : {53} client block at 0x0976EFB0, subtype 0, 64 bytes long.
a CDynLinkLibrary object at $0976EFB0, 64 bytes long
{52} client block at 0x0976CFB0, subtype 0, 64 bytes long.
a CDynLinkLibrary object at $0976CFB0, 64 bytes long
{50} client block at 0x09768FB0, subtype 0, 64 bytes long.
a CDynLinkLibrary object at $09768FB0, 64 bytes long
Object dump complete.
What am I doing wrong with the CList allocation? Thanks in advance!
Those aren't leaks. They're active allocations. Leaks will appear in the
output window when you terminate your program, but MFC makes the mistake of
calling _CrtDumpMemoryLeaks itself, which can produce spurious leak reports
in some case. See this message for more on that:

http://groups.google.com/groups?selm=4umsgsg49arlt9mnegr0f0mq623h13k0n1%404ax.com

So I guess my question is, why do you think you have a memory leak?
--
Doug Harrison
Microsoft MVP - Visual C++
Loading...