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 8 8198 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
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 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
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 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 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
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
<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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Sergey Tursanov |
last post by:
Hello all !
When I use code below and check my program with various memory
leak detection tools I get message about memory leaks in stl_alloc.h.
#include <string>
int main()
{
std::string s...
|
by: Angus Leeming |
last post by:
Hello,
Could someone explain to me why the Standard conveners chose to typedef
std::string rather than derive it from std::basic_string<char, ...>?
The result of course is that it is...
|
by: Axel |
last post by:
Hiho,
how can I reserve enough memory for a long string? I tried with
max_size() but 4GB is to much for my system, an exception ist the
result. And I'm getting an exception too when using the...
|
by: Christopher Benson-Manica |
last post by:
Let's say I have a std::string, and I want to replace all the ','
characters with " or ", i.e. "A,B,C" -> "A or B or C". Is the
following the best way to do it?
int idx;
while(...
|
by: qazmlp |
last post by:
Is there a memory leak here? Can't the memory deallocation happen
automatically as the std::string object anyway is owning the memory?
#include <cstring>
#include <string>
#include <iostream>
...
|
by: Jason Heyes |
last post by:
Does this function need to call eof after the while-loop to be correct?
bool read_file(std::string name, std::string &s)
{
std::ifstream in(name.c_str());
if (!in.is_open())
return false;
...
|
by: Erik Wikström |
last post by:
First of all, forgive me if this is the wrong place to ask this question,
if it's a stupid question (it's my second week with C++), or if this is
answered some place else (I've searched but not...
|
by: Patrick Kowalzick |
last post by:
Dear NG,
I would like to change the allocator of e.g. all std::strings, without
changing my code. Is there a portable solution to achieve this?
The only nice solution I can think of, would be...
|
by: Nemok |
last post by:
Hi,
I am new to STD so I have some questions about std::string because I
want use it in one of my projects instead of CString.
1. Is memory set dinamicaly (like CString), can I define for...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM)
The start time is equivalent to 19:00 (7PM) in Central...
|
by: erikbower65 |
last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps:
1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal.
2. Connect to...
|
by: erikbower65 |
last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA:
1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
|
by: kcodez |
last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM)
The start time is equivalent to 19:00 (7PM) in Central...
|
by: Taofi |
last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same
This are my field names
ID, Budgeted, Actual, Status and Differences
...
|
by: DJRhino1175 |
last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this -
If...
|
by: lllomh |
last post by:
Define the method first
this.state = {
buttonBackgroundColor: 'green',
isBlinking: false, // A new status is added to identify whether the button is blinking or not
}
autoStart=()=>{
|
by: DJRhino |
last post by:
Was curious if anyone else was having this same issue or not....
I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
| |