March 3rd, 2010 09:42 AM
#1
Memory leaks at c_str() of std::string
Hello All,
I develop MFC Extension DLL and I have an problem with Memory leaks issue. I try
to use the The User-Mode Dump Heap (UMDH) tool, Umdh.exe that come with Microsoft debugging tools
After I used umdh.exe to checked the Heap I found this
#1
+ 419737 ( 875104 - 455367) 30099 allocs BackTrace3379
+ 14439 ( 30099 - 15660) BackTrace3379 allocations
ntdll!RtlpNtMakeTemporaryKey+000074CE
ntdll!LdrAlternateResourcesEnabled+00002B05
ntdll!RtlDosSearchPath_Ustr+00000310
MSVCR90!malloc+00000079
mfc90!???+00000000 : 78637550
mfc90!???+00000000 : 7862F967
mfc90!???+00000000 : 7862FA1E
mfc90!???+00000000 : 7862FA75
mfc90!???+00000000 : 7862F704
mfc90!???+00000000 : 7862F636
mfc90!???+00000000 : 7862F650
mfc90!???+00000000 : 7862F66C
mfc90!???+00000000 : 786350AD
MyDLL!CParser::ParseRecord3+00000A7C (x:\projects\folder\branchs\folder2008\folder2008_newway\source\Parser.cpp, 333)
and I look into the line of code and found this
p_RecordSummaryImp->OperValue = (*iter).GetFormattedValue().c_str();
the full code is look something like below
for (VDetail::const_iterator iter = (*pfo).begin(); iter!=iterEnd; ++iter)
{
p_RecordSummaryImp->OperValue = (*iter).GetFormattedValue().c_str();
}
and (*iter).GetFormattedValue() will return the std::string type
and p_RecordSummaryImp->OperValue, the OperValue is a CString type.
My question is Why the c_str() cause the memory leaks? and how to fix it. All suggestion are welcome
-----------------------------------------------------------------------------------------------------------
#2
the stack that got from umdh.exe :
+ 433200 ( 902580 - 469380) 30086 allocs BackTrace336F
+ 14440 ( 30086 - 15646) BackTrace336F allocations
ntdll!RtlpNtMakeTemporaryKey+000074CE
ntdll!LdrAlternateResourcesEnabled+00002B05
ntdll!RtlDosSearchPath_Ustr+00000310
MSVCR90!malloc+00000079
mfc90!???+00000000 : 78637550
mfc90!???+00000000 : 7862F967
mfc90!???+00000000 : 7862FA1E
mfc90!???+00000000 : 7862FA75
mfc90!???+00000000 : 7862F704
mfc90!???+00000000 : 7862F636
mfc90!???+00000000 : 7862F650
mfc90!???+00000000 : 7862F66C
MyDLL!CParser::ParseRecord3+00000A7C (x:\projects\folder\branchs\folder2008\folder2008_newway\source\Parser.cpp, 287)
and look at the line 287, I found this code
p_RecordSummaryImp->OPort = atoi(PPacket->szSrcPort);
p_RecordSummaryImp->OPort is a int type and the PPacket->szSrcPort is TCHAR szSrcPort[64]; and PPacket is a structure type.
Why the line is cause the memory leaks problem ? or I misunderstand about the umdh.exe result that I got above. All suggestion are welcome
Thanks for your help.
Sirichai
March 3rd, 2010 09:48 AM
#2
Re: Memory leaks at c_str() of std::string
What is 'GetFormattedValue()' returning exactly? A string? Or a reference to a string? The only reason I ask is that if it's returning a string object, that object is temporary, and is destroyed after this line of code is executed. That means that any address returned by 'c_str' will be invalid after the line is done. So, 'p_RecordSummaryImp->OperValue' will contain an invalid value.
Viggy
March 3rd, 2010 10:03 AM
#3
Re: Memory leaks at c_str() of std::string
Thanks for your reply.
As for you question.
string GetFormattedValue() const { return m_strVal; };
and
private:
string m_strVal;
March 3rd, 2010 10:08 AM
#4
Re: Memory leaks at c_str() of std::string
That's what I suspected. The return value is a copy of the string, and is temporary. After the line of code is executed, 'p_RecordSummaryImp->OperValue' is invalid.
Either make the return value a reference:
Code:
string &GetFormattedValue() const { return m_strVal; };
Or, make 'p_RecordSummaryImp->OperValue' a string (not a const char *).
Viggy
March 3rd, 2010 10:30 AM
#5
Re: Memory leaks at c_str() of std::string
Thanks you very much MrViggy.
Yes you are right and it's work.
And are you know about the second problem? or any suggestion?
Tags for this Thread
Posting Permissions
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
Forum Rules
Click Here to Expand Forum to Full Width
Bookmarks