Memory leak in string assigment

G

Guru

struct TIdCategory
{
string category;
string id;
};

class CMyClass: public IMyClass
{
public :
CMyClass(const string& id, const string category);
virtual ~CMyClass();
....
....
private :
TIdCategory m_userCategory;
....
};

CMyClass::CMyClass(const string& id, const string category)
{
m_userCategory.id=id;
m_userCategory.category=category; <---------------------- Here it
giving Memory Leak by Purify
}


I am creating CMyClass object by new.

1) Why it is giving Memory Leak by Purify in string assigment ?
2) Is that really MLK?
3) If it is really MLK then in id assigment why it is not giving MLK?

-ashok
 
V

Victor Bazarov

struct TIdCategory
{
string category;
string id;
};

class CMyClass: public IMyClass
{
public :
CMyClass(const string& id, const string category);
virtual ~CMyClass();
....
....
private :
TIdCategory m_userCategory;
....
};

CMyClass::CMyClass(const string& id, const string category)

Why is 'id' passed by ref-to-const and 'category' by value?
{
m_userCategory.id=id;
m_userCategory.category=category;<---------------------- Here it
giving Memory Leak by Purify
}


I am creating CMyClass object by new.

1) Why it is giving Memory Leak by Purify in string assigment ?

Who knows? I've not used Purify in years, and don't recall what exactly
it reports. Does it say whether it's the member of 'm_userCatergory'
that leaks or the argument 'category'?
2) Is that really MLK?

What's MLK? Martin Luther King? You think his ghost affects Purify's
behavior?
3) If it is really MLK then in id assigment why it is not giving MLK?

-ashok

V
 
G

Gert-Jan de Vos

struct TIdCategory
{
   string category;
   string id;

};

class CMyClass: public IMyClass
{
    public :
    CMyClass(const string& id, const string category);
    virtual ~CMyClass();
    ....
    ....
    private :
    TIdCategory m_userCategory;
    ....

};

CMyClass::CMyClass(const string& id, const string category)
{
   m_userCategory.id=id;
   m_userCategory.category=category;  <---------------------- Here it
giving Memory Leak by Purify

}

I am creating CMyClass object by new.
^^^

Then there is your problem. Do you have to use new? If so, then do you
assign the resulting pointer to a smart pointer that takes care of the
delete? If not, then you will probably leak the CMyClass instance
sooner or later.
 
G

Guru

It was just typo.

Both are const string&
One more point , for 'id' also purify is giving MLK.


I found the for higher version of gcc GLIBCXX_FORCE_NEW flag is
available. But after using this flag also i am getting same result
 
A

Alf P. Steinbach

It was just typo.

Please quote what you're referring to.

Both are const string&

Please always copy and paste code, avoiding human error.

Also, please post *complete* but minimal example of the problem.

Please see the FAQ item on how to ask about code that doesn't work.

One more point , for 'id' also purify is giving MLK.

Please define your terms.

We are not telepaths.

I found the for higher version of gcc GLIBCXX_FORCE_NEW flag is
available. But after using this flag also i am getting same result

Presumably you have some silly error such as violating the law of three or
something. Perhaps you have copying defined in terms of assignment and have
forgotten to provide suitable copy constructor. Or something like that, or as
mentioned else-thread perhaps a false positive -- we can't know or even properly
guess from snippets of code that do not include the relevant stuff.


Cheers & hth.,

- Alf
 
D

Daniel T.

It was just typo.

Both are const string&
One more point , for 'id' also purify is giving MLK.

I found the for higher version of gcc GLIBCXX_FORCE_NEW flag is
available. But after using this flag also i am getting same result

As Gert-Jan de Vos already pointed out, you are leaking your CMyClass
object and therefore you are leaking all the sub-objects within it.
 

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

Similar Threads


Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top