Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Potential Memory Leak problem in std::string in C++

Reply
Thread Tools

Potential Memory Leak problem in std::string in C++

 
 
vidya.bhagwath@gmail.com
Guest
Posts: n/a
 
      08-11-2006
Hello Experts,
I am using std::string object as a member variable in one of the my
class. The same class member function operates on the std::string
object and it appends some string to that object. My sample code is as
follows.

["CTestMemoryLeak" is the class name and "GiveString" is the function
which appends string to the std::string object. Here "m_String" is the
std::string object.]

..h file content----------
#include <stdio.h>
#include <string>

class CTestMemoryLeak
{
public:
CTestMemoryLeak();
~CTestMemoryLeak();
std::string m_String;
void GetString();
void GiveString(std::string& m_String);

};

..cpp file content--------------------
void CTestMemoryLeak::GetString()
{
GiveString(m_String);
}

void CTestMemoryLeak::GiveString(std::string& m_String)
{
short snCount = 4;

do
{
m_String.append("TestMemoryLeak");
snCount--;
}while(snCount!=0);
}

I am testing this code using Rational TestRT and I am getting
"Potential Memory leak". But in the above example if i use the local
std::string object instead of the "m_String", which is the member
variable of my class "CTestMemoryLeak" I am not getting any Potential
memory leak. Can anybody tell me the reason for this?

I am using VC6.0 and Windows XP.

Thanks in advance for any help.

Thanks & Regards,
Vidya Bhagwath

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      08-12-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> I am using std::string object as a member variable in one of the my
> class. The same class member function operates on the std::string
> object and it appends some string to that object. My sample code is as
> follows.
>
> ["CTestMemoryLeak" is the class name and "GiveString" is the function
> which appends string to the std::string object. Here "m_String" is the
> std::string object.]
>
> .h file content----------
> #include <stdio.h>
> #include <string>
>
> class CTestMemoryLeak
> {
> public:
> CTestMemoryLeak();
> ~CTestMemoryLeak();
> std::string m_String;
> void GetString();
> void GiveString(std::string& m_String);
>
> };
>
> .cpp file content--------------------
> void CTestMemoryLeak::GetString()
> {
> GiveString(m_String);


So, you're calling 'GiveString' with the member...

> }
>
> void CTestMemoryLeak::GiveString(std::string& m_String)
> {


And here you're hiding the member m_String behind the argument
with the same name...

> short snCount = 4;
>
> do
> {
> m_String.append("TestMemoryLeak");
> snCount--;
> }while(snCount!=0);
> }
>
> I am testing this code using Rational TestRT and I am getting
> "Potential Memory leak".


So? First of all, do you trust them? Second, if you do, they only
report a *potential* leak, not a *real* one, don't they? So, why worry?

> But in the above example if i use the local
> std::string object instead of the "m_String", which is the member
> variable of my class "CTestMemoryLeak" I am not getting any Potential
> memory leak. Can anybody tell me the reason for this?


Somebody at IBM Rational should be able to. The code you posted is
not a complete program (lacking 'main' function). And even if it were,
leaks are compiler-specific, unless you do something drastic like

int *a = new int(42);
a = new int[666];
a = new int(42);

(here you lose the values of two pointers obtained from 'new' and
'new[]', which you can't restore to delete (and delete[]). Those are
true leaks. The code you posted has *no pointers*, at least in the
clear, so we can't conclude anything.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


 
Reply With Quote
 
 
 
 
vidya.bhagwath@gmail.com
Guest
Posts: n/a
 
      08-12-2006
Victor Bazarov wrote:
> (E-Mail Removed) wrote:
> > I am using std::string object as a member variable in one of the my
> > class. The same class member function operates on the std::string
> > object and it appends some string to that object. My sample code is as
> > follows.
> >
> > ["CTestMemoryLeak" is the class name and "GiveString" is the function
> > which appends string to the std::string object. Here "m_String" is the
> > std::string object.]
> >
> > .h file content----------
> > #include <stdio.h>
> > #include <string>
> >
> > class CTestMemoryLeak
> > {
> > public:
> > CTestMemoryLeak();
> > ~CTestMemoryLeak();
> > std::string m_String;
> > void GetString();
> > void GiveString(std::string& m_String);
> >
> > };
> >
> > .cpp file content--------------------
> > void CTestMemoryLeak::GetString()
> > {
> > GiveString(m_String);

>
> So, you're calling 'GiveString' with the member...
>
> > }
> >
> > void CTestMemoryLeak::GiveString(std::string& m_String)
> > {

>
> And here you're hiding the member m_String behind the argument
> with the same name...
>
> > short snCount = 4;
> >
> > do
> > {
> > m_String.append("TestMemoryLeak");
> > snCount--;
> > }while(snCount!=0);
> > }
> >
> > I am testing this code using Rational TestRT and I am getting
> > "Potential Memory leak".

>
> So? First of all, do you trust them? Second, if you do, they only
> report a *potential* leak, not a *real* one, don't they? So, why worry?
>
> > But in the above example if i use the local
> > std::string object instead of the "m_String", which is the member
> > variable of my class "CTestMemoryLeak" I am not getting any Potential
> > memory leak. Can anybody tell me the reason for this?

>
> Somebody at IBM Rational should be able to. The code you posted is
> not a complete program (lacking 'main' function). And even if it were,
> leaks are compiler-specific, unless you do something drastic like
>
> int *a = new int(42);
> a = new int[666];
> a = new int(42);
>
> (here you lose the values of two pointers obtained from 'new' and
> 'new[]', which you can't restore to delete (and delete[]). Those are
> true leaks. The code you posted has *no pointers*, at least in the
> clear, so we can't conclude anything.
>
> V
> --
> Please remove capital 'A's when replying by e-mail
> I do not respond to top-posted replies, please don't ask


Dear Victor Bazarov,
Thanks alot for ur answer.
I am posting main function here.
void main()
{

CTestMemoryLeak Obj0;
Obj0.GetString();
}

Of course it is giving me a *Potential memory leak* and not a *real*
one. But I want to know if is something related with std::string or
not and as u said I am not using any pointers in my code.

Thanks & Regards,
Vidya Bhagwath

 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      08-13-2006
(E-Mail Removed) wrote:
> [..void main..]


First off, it's "int main", learn it by heart and never write
"void main" again.

Second, as I said before, please contact Rational and ask them to
educate you how to use their product. We cannot help you, if you
need an explanation about a product. We only talk about the C++
*language* and not about any specific tools here.

> Of course it is giving me a *Potential memory leak* and not a *real*
> one. But I want to know if is something related with std::string or
> not and as u said I am not using any pointers in my code.


Again, the Rational product you're using is giving you the message.
We here cannot explain it, only folks who made it can. It probably
is related to std::string because if you don't use std::string in
your program, the message will most likely go away. But there is
no built-in potential memory leak (or something like that) in the
class 'std::string', if that's what you're asking.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


 
Reply With Quote
 
vidya.bhagwath@gmail.com
Guest
Posts: n/a
 
      08-14-2006
Victor Bazarov wrote:
> (E-Mail Removed) wrote:
> > [..void main..]

>
> First off, it's "int main", learn it by heart and never write
> "void main" again.
>
> Second, as I said before, please contact Rational and ask them to
> educate you how to use their product. We cannot help you, if you
> need an explanation about a product. We only talk about the C++
> *language* and not about any specific tools here.
>
> > Of course it is giving me a *Potential memory leak* and not a *real*
> > one. But I want to know if is something related with std::string or
> > not and as u said I am not using any pointers in my code.

>
> Again, the Rational product you're using is giving you the message.
> We here cannot explain it, only folks who made it can. It probably
> is related to std::string because if you don't use std::string in
> your program, the message will most likely go away. But there is
> no built-in potential memory leak (or something like that) in the
> class 'std::string', if that's what you're asking.
>
> V
> --
> Please remove capital 'A's when replying by e-mail
> I do not respond to top-posted replies, please don't ask


Dear Victor Bazarov,
> First off, it's "int main", learn it by heart and never write
> "void main" again.


Thanks for correcting me. I agree with u.

> Second, as I said before, please contact Rational and ask them to
> educate you how to use their product. We cannot help you, if you
> need an explanation about a product. We only talk about the C++
> *language* and not about any specific tools here.


I am not expecting any explanation regarding Rational TestRT product. I
mentioned name of that product because I am using that tool to test my
code. I know this group only talks about C++.


> Again, the Rational product you're using is giving you the message.
> We here cannot explain it, only folks who made it can. It probably
> is related to std::string because if you don't use std::string in
> your program, the message will most likely go away. But there is
> no built-in potential memory leak (or something like that) in the
> class 'std::string', if that's what you're asking.


My question is related with "std::string" and not with the tool usage.
But u cleared my doubt by saying that *std::string has no built-in
potential memory leak* .

Thanks & Regards,
Vidya Bhagwath

 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      08-14-2006
(E-Mail Removed) wrote:
> [..]
> My question is related with "std::string" and not with the tool usage.
> But u cleared my doubt by saying that *std::string has no built-in
> potential memory leak* .


Vidya,

I still can see some things that make me think I've not delivered my
point correctly somehow.

(a) I am not sure who that 'u' is you keep referring to. You said you
agreed "with u" and that "u cleared" your doubt...

(b) Please understand this: 'std::string' as we can discuss it here, is
a bunch of *definitions*. 'std::string' as you see [mis]behaving in
your program comes from *a particular implementation* of the library,
and that's why you need to talk to the makers of that library (you
can find out who that is from the makers of your compiler), and ask
them why your Rational tool might want to report a potential memory
leak when std::string is used in your program. Another possibility
is to talk to Rational people and ask them why their tool might
report a potential memory leak in your program, with the library
implementation coming from those who made it. I am sure Rational
folks either have already tested their tool with that implementation
or can easily do that.

I am not sure how I can get it less unclear, but do ask questions if you
have any left.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


 
Reply With Quote
 
vidya.bhagwath@gmail.com
Guest
Posts: n/a
 
      08-16-2006
Victor Bazarov wrote:
> (E-Mail Removed) wrote:
> > [..]
> > My question is related with "std::string" and not with the tool usage.
> > But u cleared my doubt by saying that *std::string has no built-in
> > potential memory leak* .

>
> Vidya,
>
> I still can see some things that make me think I've not delivered my
> point correctly somehow.
>
> (a) I am not sure who that 'u' is you keep referring to. You said you
> agreed "with u" and that "u cleared" your doubt...
>
> (b) Please understand this: 'std::string' as we can discuss it here, is
> a bunch of *definitions*. 'std::string' as you see [mis]behaving in
> your program comes from *a particular implementation* of the library,
> and that's why you need to talk to the makers of that library (you
> can find out who that is from the makers of your compiler), and ask
> them why your Rational tool might want to report a potential memory
> leak when std::string is used in your program. Another possibility
> is to talk to Rational people and ask them why their tool might
> report a potential memory leak in your program, with the library
> implementation coming from those who made it. I am sure Rational
> folks either have already tested their tool with that implementation
> or can easily do that.
>
> I am not sure how I can get it less unclear, but do ask questions if you
> have any left.
>
> V
> --
> Please remove capital 'A's when replying by e-mail
> I do not respond to top-posted replies, please don't ask


Victor Bazarov,

> (a) I am not sure who that 'u' is you keep referring to. You said you
> agreed "with u" and that "u cleared" your doubt...


Here "u" means Victor Bazarov. In my previous post I started my answer
by addressing Victor Bazarov only.

> (b) Please understand this: 'std::string' as we can discuss it here, is
> a bunch of *definitions*. 'std::string' as you see [mis]behaving in
> your program comes from *a particular implementation* of the library,
> and that's why you need to talk to the makers of that library (you
> can find out who that is from the makers of your compiler), and ask
> them why your Rational tool might want to report a potential memory
> leak when std::string is used in your program. Another possibility
> is to talk to Rational people and ask them why their tool might
> report a potential memory leak in your program, with the library
> implementation coming from those who made it. I am sure Rational
> folks either have already tested their tool with that implementation
> or can easily do that.
>


Victor Bazarov thanks alot for the detailed explanation and suggestion.
I will ask question if I have any further doubt.

Thanks & Regards,
Vidya Bhagwath

 
Reply With Quote
 
vidya.bhagwath@gmail.com
Guest
Posts: n/a
 
      08-16-2006
Victor Bazarov wrote:
> (E-Mail Removed) wrote:
> > [..]
> > My question is related with "std::string" and not with the tool usage.
> > But u cleared my doubt by saying that *std::string has no built-in
> > potential memory leak* .

>
> Vidya,
>
> I still can see some things that make me think I've not delivered my
> point correctly somehow.
>
> (a) I am not sure who that 'u' is you keep referring to. You said you
> agreed "with u" and that "u cleared" your doubt...
>
> (b) Please understand this: 'std::string' as we can discuss it here, is
> a bunch of *definitions*. 'std::string' as you see [mis]behaving in
> your program comes from *a particular implementation* of the library,
> and that's why you need to talk to the makers of that library (you
> can find out who that is from the makers of your compiler), and ask
> them why your Rational tool might want to report a potential memory
> leak when std::string is used in your program. Another possibility
> is to talk to Rational people and ask them why their tool might
> report a potential memory leak in your program, with the library
> implementation coming from those who made it. I am sure Rational
> folks either have already tested their tool with that implementation
> or can easily do that.
>
> I am not sure how I can get it less unclear, but do ask questions if you
> have any left.
>
> V
> --
> Please remove capital 'A's when replying by e-mail
> I do not respond to top-posted replies, please don't ask


Victor Bazarov,

> (a) I am not sure who that 'u' is you keep referring to. You said you
> agreed "with u" and that "u cleared" your doubt...


Here "u" means Victor Bazarov. In my previous post I started my answer
by addressing Victor Bazarov only.

> (b) Please understand this: 'std::string' as we can discuss it here, is
> a bunch of *definitions*. 'std::string' as you see [mis]behaving in
> your program comes from *a particular implementation* of the library,
> and that's why you need to talk to the makers of that library (you
> can find out who that is from the makers of your compiler), and ask
> them why your Rational tool might want to report a potential memory
> leak when std::string is used in your program. Another possibility
> is to talk to Rational people and ask them why their tool might
> report a potential memory leak in your program, with the library
> implementation coming from those who made it. I am sure Rational
> folks either have already tested their tool with that implementation
> or can easily do that.
>


Victor Bazarov thanks alot for the detailed explanation and suggestion.
I will ask question if I have any further doubt.

Thanks & Regards,
Vidya Bhagwath

 
Reply With Quote
 
Jim Langston
Guest
Posts: n/a
 
      08-16-2006
<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> Victor Bazarov wrote:
>> (E-Mail Removed) wrote:
>> > [..]
>> > My question is related with "std::string" and not with the tool usage.
>> > But u cleared my doubt by saying that *std::string has no built-in
>> > potential memory leak* .

>>
>> Vidya,
>>
>> I still can see some things that make me think I've not delivered my
>> point correctly somehow.
>>
>> (a) I am not sure who that 'u' is you keep referring to. You said you
>> agreed "with u" and that "u cleared" your doubt...
>>
>> (b) Please understand this: 'std::string' as we can discuss it here, is
>> a bunch of *definitions*. 'std::string' as you see [mis]behaving in
>> your program comes from *a particular implementation* of the library,
>> and that's why you need to talk to the makers of that library (you
>> can find out who that is from the makers of your compiler), and ask
>> them why your Rational tool might want to report a potential memory
>> leak when std::string is used in your program. Another possibility
>> is to talk to Rational people and ask them why their tool might
>> report a potential memory leak in your program, with the library
>> implementation coming from those who made it. I am sure Rational
>> folks either have already tested their tool with that implementation
>> or can easily do that.
>>
>> I am not sure how I can get it less unclear, but do ask questions if you
>> have any left.
>>
>> V
>> --
>> Please remove capital 'A's when replying by e-mail
>> I do not respond to top-posted replies, please don't ask

>
> Victor Bazarov,
>
>> (a) I am not sure who that 'u' is you keep referring to. You said you
>> agreed "with u" and that "u cleared" your doubt...

>
> Here "u" means Victor Bazarov. In my previous post I started my answer
> by addressing Victor Bazarov only.


I don't know what language you speak, but I speak English. In English "you"
would mean Victor Bazarov, not "u" which is meaningless.

>
>> (b) Please understand this: 'std::string' as we can discuss it here, is
>> a bunch of *definitions*. 'std::string' as you see [mis]behaving in
>> your program comes from *a particular implementation* of the library,
>> and that's why you need to talk to the makers of that library (you
>> can find out who that is from the makers of your compiler), and ask
>> them why your Rational tool might want to report a potential memory
>> leak when std::string is used in your program. Another possibility
>> is to talk to Rational people and ask them why their tool might
>> report a potential memory leak in your program, with the library
>> implementation coming from those who made it. I am sure Rational
>> folks either have already tested their tool with that implementation
>> or can easily do that.
>>

>
> Victor Bazarov thanks alot for the detailed explanation and suggestion.
> I will ask question if I have any further doubt.
>
> Thanks & Regards,
> Vidya Bhagwath
>



 
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
Will this be a potential memory leak? cyl Perl Misc 3 11-13-2008 07:21 AM
Memory leak even after deleting memory pointers from vector cham C++ 5 09-25-2008 10:30 AM
Leak or no leak ?? Richard Heathfield C Programming 4 07-10-2006 11:37 AM
Dynamic memory allocation and memory leak... s.subbarayan C Programming 10 03-22-2005 02:48 PM
Potential memory leak using BufferedImage and ImageIO in a servlet? Richard Corfield Java 0 03-04-2004 10:52 AM



Advertisments