Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

new CString memory leak

163 views
Skip to first unread message

Michael De Blasio

unread,
Jun 20, 2001, 8:15:34 AM6/20/01
to
Hi, is there any body who has encountered the memory leak
that occurse when you use the new command to dynamically
allocate a CString object?

CString pStr = new CString;

I have found thatthe AllocBuffer function in the CString
class does not get called when using the new command but
it does when you do:

CString cStr;

This is a bug and I have not been able to fix it. How do I
get MS aware of this problem and get them to fix it?

Michael De Blasio

wardo

unread,
Jun 20, 2001, 9:25:57 AM6/20/01
to
CString pStr = new CString;
needs to be:
CString *pStr = new CString;

HTH
wardo


"Michael De Blasio" <debl...@saic.com> wrote in message
news:043101c0f982$b4ff7880$9be62ecf@tkmsftngxa03...

Jim Howard

unread,
Jun 20, 2001, 10:57:52 AM6/20/01
to
The buffer won't be allocated until you put something in the string. There
is no leak. And don't forget the '*'.

Jim


"Michael De Blasio" <debl...@saic.com> wrote in message
news:043101c0f982$b4ff7880$9be62ecf@tkmsftngxa03...

Joseph M. Newcomer

unread,
Jun 20, 2001, 3:20:10 PM6/20/01
to
It isn't their problem, it is yours. In fact, I don't know how the
code can compile, since you need to say
CString * pStr = new CString;

However, if I assume you made a typo and meant to write the above,
there is still not an issue. The destructor of the string will free
the buffer. This means that if you do a new you *must* do a delete.

Can you say more about what you are doing with the string? Note that
if you are putting the CString * into the itemdata of a listbox,
nothing will free it up; you need to have an owner-draw listbox and
implement an OnDeleteItem handler. If you are storing it in a class,
the class is responsible for deleting the string in its destructor.
joe

Joseph M. Newcomer [MVP]
email: newc...@flounder.com
Web: http://www3.pgh.net/~newcomer
MVP Tips: http://www3.pgh.net/~newcomer/mvp_tips.htm

Michael De Blasio

unread,
Jun 21, 2001, 8:36:27 AM6/21/01
to
The CString pStr = new CString was a typo error.
I did use CString * pStr = new CString;.

I now have more information on this problem. The memory
leak occurrs when I dynamically allocate CString that are
then placed into a CStringList. Here is a code sample that
produces the problem.

int iMaxListSize = 3;
CStringList docList;
CString *pStr, *pStr1, *pStr2;
POSITION pos;
for ( long i = 0; i < 100000; i++ )
{ if ( docList.GetCount() >= iMaxListSize )
{ pos = docList.GetHeadPosition();
pStr1 = (CString *) &docList.GetAt(pos);
// CString is also destroyed by RemvoeHead()
pStr2 = (CString *) &docList.RemoveHead();
ASSERT ( pStr1 == pStr2 );// verify what was in
the head
}
pStr = new CString(_T("This is a test."));
POSITION pos = docList.AddTail((const CString&) *pStr);
ASSERT( pos != NULL );
}
docList.RemoveAll();

Michael De Blasio

wardo

unread,
Jun 21, 2001, 9:27:21 AM6/21/01
to
Functions from the CStringList such as RemoveHead, RemoveTail, RemoveAll
only remove the pointer from the list:
MFC DOC for CStringList::RemoveAll() states:
"When you remove elements from a CObList, you remove the object pointers
from the list. It is your responsibility to delete the objects themselves."

You do a lot of "new" but no deletes... that's where the leak is...

HTH,wardo

"Michael De Blasio" <debl...@saic.com> wrote in message
news:0ff101c0fa4e$ca2e29f0$9be62ecf@tkmsftngxa03...

Joseph M. Newcomer

unread,
Jun 21, 2001, 11:27:50 PM6/21/01
to
Yep, I knew it was not Microsoft's problem! Check out the section on
type-safe arrays and study the helper functions for things like item
deletion.
joe

Joseph M. Newcomer [MVP]

Sam Hobbs

unread,
Jun 22, 2001, 7:11:53 PM6/22/01
to
As you say, if a CString is allocated then it must be explicitly deleted
also; CStringList will not delete it. However, I think that the MFC
documentation of the olde collection classes is quite poor and this is an
example of the problem. There is no "MFC DOC for CStringList::RemoveAll()".
The page you get when you try to get that documentation is the page for
CObList::RemoveAll, which is misleading, since CStringList stores CString
objects, not pointers to them. So that means that it is not necessary to
allocate a new CString for each entry; a single CString can be used, and
elements will be copied when they are added. Knowing that, CStringList is
very easy to use. RemoveAll removes all the strings. CStringList can be used
without a single "new".

The following is my page showing samples of CStringList but it does not say
much more.

http://www.cpp.atfreeweb.com/Collections/CStringList.html


"wardo" <wa...@provisionconsulting.com> wrote in message
news:AvmY6.20636$9r1.1...@e3500-atl1.usenetserver.com...

Morten Wennevik

unread,
Jun 24, 2001, 12:45:38 PM6/24/01
to

for( pos = stringlist.GetHeadPosition(); pos != 0; )
{
CString* pStr = (CString*)stringlist.GetNext(pos);
delete pStr;
}
stringlist.EmptyAll();

wardo

unread,
Jun 24, 2001, 5:29:22 PM6/24/01
to
Good info... Thanks!... I presume then when you do a RemoveHead/Tail you get
a CString not a CString*? I haven't used CStringList/Array in a while, but
it's good to keep up on it...
Thanks! :)
wardo

"Sam Hobbs" <sam...@socal.rr.com> wrote in message
news:uKJwrC3#AHA.408@tkmsftngp03...

Sam Hobbs

unread,
Jun 24, 2001, 9:06:45 PM6/24/01
to
Fortunately Microsoft provides the source for MFC. Actually, we only need
the AFXCOLL.H header for the definition of RemoveHead and RemoveTail to
answer your question.

CString RemoveHead();
CString RemoveTail();


"wardo" <wa...@provisionconsulting.com> wrote in message

news:mRsZ6.31936$9r1.2...@e3500-atl1.usenetserver.com...

0 new messages