Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > STL string, need to free the memory?

Reply
Thread Tools

STL string, need to free the memory?

 
 
olan@freesurf.fr
Guest
Posts: n/a
 
      08-14-2005
Hi
I'm kinda new at this, but...

I have a class with a STL string m_strText
and a member function that sets the text :

void CTest::SetString( char *pszText )
{
m_strText = pszText;
}

When my program exits, I have a memory leak that corresponds
to the text in m_strText.

Do I have to do something special to free up the memory?
I didn't see any functions like that in the STL string docs.
Thanks
Fred

PS this is with MSVC++ 6.0

 
Reply With Quote
 
 
 
 
Mirek Fidler
Guest
Posts: n/a
 
      08-14-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> Hi
> I'm kinda new at this, but...
>
> I have a class with a STL string m_strText
> and a member function that sets the text :
>
> void CTest::SetString( char *pszText )
> {
> m_strText = pszText;
> }
>
> When my program exits, I have a memory leak that corresponds
> to the text in m_strText.
>
> Do I have to do something special to free up the memory?


Make sure that you destroy CTest.

Mirek
 
Reply With Quote
 
 
 
 
Dave Townsend
Guest
Posts: n/a
 
      08-14-2005

"Mirek Fidler" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> (E-Mail Removed) wrote:
> > Hi
> > I'm kinda new at this, but...
> >
> > I have a class with a STL string m_strText
> > and a member function that sets the text :
> >
> > void CTest::SetString( char *pszText )
> > {
> > m_strText = pszText;
> > }
> >
> > When my program exits, I have a memory leak that corresponds
> > to the text in m_strText.
> >
> > Do I have to do something special to free up the memory?

>
> Make sure that you destroy CTest.
>
> Mirek


It looks to me that the pszText is the guy who should be deleted, STL
string will make a
copy of the char* given to it.


 
Reply With Quote
 
Cy Edmunds
Guest
Posts: n/a
 
      08-15-2005
<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ups.com...
> Hi
> I'm kinda new at this, but...
>
> I have a class with a STL string m_strText
> and a member function that sets the text :
>
> void CTest::SetString( char *pszText )


There's a lot to not like about this signature. First of all, it uses the
dreaded Hungarian notation. (Of course if your workgroup uses it, nothing
can be done.) Secondly, it doesn't change what pszText points to so the
argument should be declared const. Thirdly, it is really more convenient to
make the argument (const std::string &strText). If you do that, anybody who
uses this function can still use a const char * because it will
automatically be converted to a std::string. However, if somebody wants to
use a std::string, say "s", he won't have to write
my_CTest.SetString(s.c_str()); which takes a std::string, extracts a const
char *, and converts it back to a std::string. ugh.

> {
> m_strText = pszText;
> }
>
> When my program exits, I have a memory leak that corresponds
> to the text in m_strText.


Look elsewhere. std::string cleans up its own memory. If you got the string
using operator new() you may not have made a corresponding call to operator
delete(). If you created the CTest object with operator new(), the same
problem may have arisen.

>
> Do I have to do something special to free up the memory?
> I didn't see any functions like that in the STL string docs.
> Thanks
> Fred
>
> PS this is with MSVC++ 6.0
>


--
Cy
http://home.rochester.rr.com/cyhome/


 
Reply With Quote
 
olan@freesurf.fr
Guest
Posts: n/a
 
      08-15-2005
Sorry about the Hungarian.... regarding const etc this is just a quick
test program to try to find out where the leaks come from so I didnt
strive for perfection.

anyway the complete program is

#include <windows.h>
#include <crtdbg.h>
#include "CTest.h"

CTest Test;

int main( int argc, char *argv[] )
{
std::string strBlabla;

Test.SetString( "blabla" );
strBlabla = Test.GetString();

printf( "the string is %s\n", strBlabla.c_str() );

_CrtDumpMemoryLeaks();
return 0;
}

if I comment out the SetString call I have no leaks
When the SetString call runs I have leaks
This is a pretty simple program and yet I don't see where the leak
could come from
Thanks
Fred

 
Reply With Quote
 
Tom Felker
Guest
Posts: n/a
 
      08-15-2005
On Sun, 14 Aug 2005 20:37:03 -0700, olan wrote:

> Sorry about the Hungarian.... regarding const etc this is just a quick
> test program to try to find out where the leaks come from so I didnt
> strive for perfection.
>
> anyway the complete program is
>
> #include <windows.h>
> #include <crtdbg.h>
> #include "CTest.h"
>
> CTest Test;
>
> int main( int argc, char *argv[] )
> {
> std::string strBlabla;
>
> Test.SetString( "blabla" );
> strBlabla = Test.GetString();
>
> printf( "the string is %s\n", strBlabla.c_str() );
>
> _CrtDumpMemoryLeaks();
> return 0;
> }
>
> if I comment out the SetString call I have no leaks
> When the SetString call runs I have leaks
> This is a pretty simple program and yet I don't see where the leak
> could come from
> Thanks
> Fred


The strBlabla object's destructor (where it would deallocate its memory)
doesn't get called until control passes out of the block in which it is
declared, in this case, main(). If you move everything except the last
two lines into a new function that main calls, then you won't call
_CrtDumpMemoryLeaks until that new function returns, and thus, you
shouldn't see the "leak."

--
Tom Felker, <(E-Mail Removed)>
<http://vlevel.sourceforge.net> - Stop fiddling with the volume knob.

Try not! Do! or do not. There is no try.
-- Yoda

 
Reply With Quote
 
Greg
Guest
Posts: n/a
 
      08-15-2005

Tom Felker wrote:
> On Sun, 14 Aug 2005 20:37:03 -0700, olan wrote:
>
> > Sorry about the Hungarian.... regarding const etc this is just a quick
> > test program to try to find out where the leaks come from so I didnt
> > strive for perfection.
> >
> > anyway the complete program is
> >
> > #include <windows.h>
> > #include <crtdbg.h>
> > #include "CTest.h"
> >
> > CTest Test;
> >
> > int main( int argc, char *argv[] )
> > {
> > std::string strBlabla;
> >
> > Test.SetString( "blabla" );
> > strBlabla = Test.GetString();
> >
> > printf( "the string is %s\n", strBlabla.c_str() );
> >
> > _CrtDumpMemoryLeaks();
> > return 0;
> > }
> >
> > if I comment out the SetString call I have no leaks
> > When the SetString call runs I have leaks
> > This is a pretty simple program and yet I don't see where the leak
> > could come from
> > Thanks
> > Fred

>
> The strBlabla object's destructor (where it would deallocate its memory)
> doesn't get called until control passes out of the block in which it is
> declared, in this case, main(). If you move everything except the last
> two lines into a new function that main calls, then you won't call
> _CrtDumpMemoryLeaks until that new function returns, and thus, you
> shouldn't see the "leak."
>
> --
> Tom Felker, <(E-Mail Removed)>
> <http://vlevel.sourceforge.net> - Stop fiddling with the volume knob.
>


Reasonable advice, but it probably won't get rid of the reported leak
since we know there is a std::string data member (m_strText) in the
CTest class. And since Test is a statically allocated CText object its
m_strText data member won't be deallocated until after main exits, when
Test is torn down.

Greg

 
Reply With Quote
 
hacker++
Guest
Posts: n/a
 
      08-15-2005

(E-Mail Removed) wrote:

> anyway the complete program is
>
> #include <windows.h>
> #include <crtdbg.h>
> #include "CTest.h"
>
> CTest Test;
>
> int main( int argc, char *argv[] )
> {


//Add additional scope here
{
> std::string strBlabla;
>
> Test.SetString( "blabla" );
> strBlabla = Test.GetString();
>
> printf( "the string is %s\n", strBlabla.c_str() );
>


// end of dummy scope
}
> _CrtDumpMemoryLeaks();
> return 0;
> }
>


Do you still get the leak?

 
Reply With Quote
 
Geo
Guest
Posts: n/a
 
      08-15-2005

(E-Mail Removed) wrote:
> Sorry about the Hungarian.... regarding const etc this is just a quick
> test program to try to find out where the leaks come from so I didnt
> strive for perfection.
>
> anyway the complete program is
>
> #include <windows.h>
> #include <crtdbg.h>
> #include "CTest.h"
>
> CTest Test;
>
> int main( int argc, char *argv[] )
> {
> std::string strBlabla;
>
> Test.SetString( "blabla" );
> strBlabla = Test.GetString();
>
> printf( "the string is %s\n", strBlabla.c_str() );
>
> _CrtDumpMemoryLeaks();
> return 0;
> }
>
> if I comment out the SetString call I have no leaks
> When the SetString call runs I have leaks
> This is a pretty simple program and yet I don't see where the leak
> could come from
> Thanks
> Fred


I don't know about other compilers, but Visual C++ runs it's leak
detection code before it destroys global objects, so all globals that
allocate memory appear to leak, move the definiton of test into main
and it should go away.

 
Reply With Quote
 
Mirek Fidler
Guest
Posts: n/a
 
      08-15-2005
> argument should be declared const. Thirdly, it is really more convenient to
> make the argument (const std::string &strText). If you do that, anybody who
> uses this function can still use a const char * because it will
> automatically be converted to a std::string. However, if somebody wants to
> use a std::string, say "s", he won't have to write
> my_CTest.SetString(s.c_str()); which takes a std::string, extracts a const
> char *, and converts it back to a std::string. ugh.


OTOH, this might bloat the code by including constructor call machinery
at each call to SetString. If you are going to use it mainly for
character constants, this might be significant....

In fact, it is really sad that for rather false reasons, C++ comitee
accepted c_str nonsense over simple operator const char *().

Mirek
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Copy elements from one STL container to another STL container Marko.Cain.23@gmail.com C++ 4 02-16-2006 05:03 PM
To STL or not to STL Allan Bruce C++ 41 10-17-2003 08:21 PM



Advertisments