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

Discussion in 'C++' started by vidya.bhagwath@gmail.com, Aug 11, 2006.

  1. Guest

    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, 2006
    #1
    1. Advertising

  2. 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, Aug 12, 2006
    #2
    1. Advertising

  3. Guest

    Victor Bazarov wrote:
    > 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, 2006
    #3
  4. 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, Aug 13, 2006
    #4
  5. Guest

    Victor Bazarov wrote:
    > 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, 2006
    #5
  6. 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, Aug 14, 2006
    #6
  7. Guest

    Victor Bazarov wrote:
    > 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, 2006
    #7
  8. Guest

    Victor Bazarov wrote:
    > 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, 2006
    #8
  9. Jim Langston Guest

    <> wrote in message
    news:...
    > Victor Bazarov wrote:
    >> 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
    >
     
    Jim Langston, Aug 16, 2006
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Richard Corfield
    Replies:
    0
    Views:
    3,533
    Richard Corfield
    Mar 4, 2004
  2. Sergey Tursanov

    Memory leak in std::string

    Sergey Tursanov, Jul 13, 2003, in forum: C++
    Replies:
    1
    Views:
    3,827
    Sergey Tursanov
    Jul 13, 2003
  3. Peter Jansson
    Replies:
    5
    Views:
    6,324
    Ivan Vecerina
    Mar 17, 2005
  4. Jeffrey Walton
    Replies:
    10
    Views:
    944
    Mathias Gaunard
    Nov 26, 2006
  5. cyl
    Replies:
    3
    Views:
    106
    Ilya Zakharevich
    Nov 13, 2008
Loading...

Share This Page