468,457 Members | 1,690 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,457 developers. It's quick & easy.

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

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

Aug 11 '06 #1
8 7761
vi************@gmail.com 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
Aug 12 '06 #2
Victor Bazarov wrote:
vi************@gmail.com 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

Aug 12 '06 #3
vi************@gmail.com 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
Aug 13 '06 #4
Victor Bazarov wrote:
vi************@gmail.com 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

Aug 14 '06 #5
vi************@gmail.com 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
Aug 14 '06 #6
Victor Bazarov wrote:
vi************@gmail.com 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

Aug 16 '06 #7
Victor Bazarov wrote:
vi************@gmail.com 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

Aug 16 '06 #8
<vi************@gmail.comwrote in message
news:11**********************@m73g2000cwd.googlegr oups.com...
Victor Bazarov wrote:
>vi************@gmail.com 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

Aug 16 '06 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Sergey Tursanov | last post: by
10 posts views Thread by Angus Leeming | last post: by
1 post views Thread by Axel | last post: by
11 posts views Thread by Christopher Benson-Manica | last post: by
22 posts views Thread by Jason Heyes | last post: by
19 posts views Thread by Erik Wikström | last post: by
8 posts views Thread by Patrick Kowalzick | last post: by
6 posts views Thread by Nemok | last post: by
1 post views Thread by subhajit12345 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.