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

V

vidya.bhagwath

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
 
V

Victor Bazarov

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
 
V

vidya.bhagwath

Victor said:
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

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
 
V

Victor Bazarov

[..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
 
V

vidya.bhagwath

Victor said:
[..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

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
 
V

Victor Bazarov

[..]
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
 
V

vidya.bhagwath

Victor said:
[..]
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

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
 
V

vidya.bhagwath

Victor said:
[..]
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

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
 
J

Jim Langston

Victor said:
[..]
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

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top