stringstream causes memory link VC++ 2005?
- This simple program seems to leak LOTS of memory when compiled in VC++ 2005:
int
_tmain(int argc, _TCHAR* argv[]){
for(int i = 0; i < 100000; ++i){
::Sleep(1);
std::basic_stringstream<TCHAR> str;
str << _T(
"Current iteration: ") << i;}
return 0;}
CRT: Multi-threaded debug or Multi-threaded
I compiled and ran the same piece of code in VC++ 2003 and I don't see ANY leaks!
Any ideas? Please help!
Thanks in advance
Answers
- It looks like you're right..
From what I traced, the basic_iostream inherits from both basic_ostream and basic_istream - both of which virtually inherit from std::basic_ios, which inherits from ios_base. In ios_base::_Init(), the first line is _Ploc = 0;, and the last line is _Ploc = _NEW_CRT locale; (this is in the file xiosbase ) - well, since _Init() gets called for basic_ostream AND basic_istream, that _Ploc pointer is shared, and gets overwritten (with the _Ploc=0 line), and assigned a new locale object - now this is SUPPOSED to be the same "global_locale" object, but it's not - it gets assigned a new locale object, and I think the original object never gets its reference count decremented, so it never gets freed...
- Could you please log a bug at http://lab.msdn.microsoft.com/productfeedback/default.aspx?
Thanks in advance for taking the time to log the issue!
Thanks,
Ayman Shoukry
VC++ Team IMPORTANT NOTE: just after I posted my own workaround, the following official workaround was posted to the bug.
This one comes from PJ Plauger (author of STL) himself:
http://connect.microsoft.com/VisualStudio/feedback/Workaround.aspx?FeedbackID=98861I'd recommend using any official workaround before mine.
For rebuilding the DLLs, there are some modifications you would likely have to do (i.e. avoid having multiple versions of the CRT in memory, if using DLLs dependent on MSVCR80.DLL, e.g. MFC).
[edit: changed URL from old Feedback Center to Microsoft Connect]- Hi,
I can tell you that we are very aware of this bug. You may see a workaround posted for this issue on MSDN feedback site, http://lab.msdn.microsoft.com/ProductFeedback/ViewWorkaround.aspx?FeedbackID=FDBK40119#1. You may use it for now before official hotfix is available.
Nikola
VC++ The bug has been fixed. And a hotfix is in process of being release to companies who requested it. A fix is most likely going to be publicly available in VS2005 SP1.
Nikola
- The new KB id is 919280
All Replies
- It looks like you're right..
From what I traced, the basic_iostream inherits from both basic_ostream and basic_istream - both of which virtually inherit from std::basic_ios, which inherits from ios_base. In ios_base::_Init(), the first line is _Ploc = 0;, and the last line is _Ploc = _NEW_CRT locale; (this is in the file xiosbase ) - well, since _Init() gets called for basic_ostream AND basic_istream, that _Ploc pointer is shared, and gets overwritten (with the _Ploc=0 line), and assigned a new locale object - now this is SUPPOSED to be the same "global_locale" object, but it's not - it gets assigned a new locale object, and I think the original object never gets its reference count decremented, so it never gets freed...
- Could you please log a bug at http://lab.msdn.microsoft.com/productfeedback/default.aspx?
Thanks in advance for taking the time to log the issue!
Thanks,
Ayman Shoukry
VC++ Team - Thanks, mike_n. Looks like you created a bug report as well.
Ayman (or any MS representative),
Any idea when we can expect to get a fix/workaround for this bug? It would suck to not be able to use stream classes (esp stringstream)!
Thanks. Could you send me the bug link and I can make sure someone look at it straight ahead? They should be able to provide at least a workaround.
Thanks,
Ayman Shoukry
VC++ Team- Hey Ayman
Here's the link to bug report that mike_n created:
http://lab.msdn.microsoft.com/productfeedback/viewfeedback.aspx?feedbackid=e08bd793-3fef-40ff-adda-ed313e0eafcc
If you can't get to the link, the Bug ID is FDBK40119.
Looking forward to the workaround...
Thanks! - Ayman (or any VC++ team member)
If/when the VC++ team comes up with a fix or a workaround, how can I (or anyone else who's interested in the workaround) expect to be notified? Should I continue to monitor the bug submission page?
Thanks in advance. - That is correct a workaround (if exists) should be posted there once the folks look into the issue.
If you don't see an update to the link in the next couple of days, feel free to ping me on the forums and I can take a look,
Thanks,
Ayman Shoukry
VC++ Team - Is there any patch or workaround for this? We just started moving our environment to VC++ 2005 (from VC++ 6.0 and VC++ 7.1(2003). Without a patch
for this issue, we will be in trouble
Thanks,
-Madhu. Hi,
Thanks for having an interest in the issue. Please use http://lab.msdn.microsoft.com/productfeedback/viewfeedback.aspx?feedbackid=e08bd793-3fef-40ff-adda-ed313e0eafcc to vote on it as well as add comments regarding needing a patch for it or a workaround. Also, you might need to contact PSS to request a private fix for it. As soon as folks look into the issue they should have more details there (at the above link)
Once more, thanks and please keep an eye on the link. From my side, I will try to get in contact with the responsible folks and point them to the bug.
Thanks,
Ayman Shoukry
VC++ Team- Thanks Ayman.. I have already voted using the link. How do I contact PSS to request a private fix?
Thanks,
Madhu. - You can contact PPS through our suport page at http://support.microsoft.com/default.aspx.
Make sure to let them aware of the bug link.
Thanks,
Ayman Shoukry
VC++ Team - Thanks Ayman..I will be contacting the support group for this..
Is there emergency patch coming out for the std::iostream memory leak..?
To me its a 'show stopper'. I can't even suggest our development team to migrate to Visual C++ 2005 by knowing the fact that there is a memory leak..!!!!
Thanks,
Madhu. As I mentioned you will have to request such patch first through PSS so that to be considered. Other wise, it might end up being fixed in the next release or service pack.
Also, it all depends on the results of the investigations that are currently being carried.
Thanks,
Ayman Shoukry
VC++ Team- std::iostream memory leak - any status update yet?
This is a "cannot ship app until it's fixed" bug - is there any fix we can apply ourselves to the standard library sources until a fix is generally available? Can someone who's looked into it please make a quick post here?
Thanks
Ted. I completely agree that is is a "cannot ship app until it's fixed" bug...Unfortunately I haven't seen any other update from Microsoft (other than the one saying that they are investigating..).
We are almost in the process dumping the Visual C++ 2005 and go back to Visual C++ 2003 version. Its very unfortunate that Microsoft is not releasing a quick patch..


Thanks,
Madhu.- I was going to attempt to come up with a workaround, but I cannot reproduce the leak. Here's my code on newly generated Console project. I am using VS 2005 Beta 2:
int
_tmain(int argc, _TCHAR* argv[])
{
for(int i = 0; i < 100000; ++i)
{
::Sleep(1);
std::basic_stringstream<TCHAR> str;
str << _T("Current iteration: ") << i;
if( i % 1000 == 0 )
{
SIZE_T min, max;
GetProcessWorkingSetSize( GetCurrentProcess(), &min, &max );
cout << "min = " << min << " max = " << max << endl;
}
}
return 0;
}
Output:
E:\tempprojects\memleak\memleak\Release>memleak
min = 204800 max = 1413120
min = 204800 max = 1413120
min = 204800 max = 1413120
min = 204800 max = 1413120
min = 204800 max = 1413120 - I have already send an email to the person driving the issue.
By the way, you can always request private fixes through PSS at http://support.microsoft.com/default.aspx
Thanks,
Ayman Shoukry
VC++ Team - Ayman is right. The bug is under investigation and work on it is prioritized according to other bugs in the database. If this is a critical bug or show-stopper for your product and you have to have a fix for it ASAP, please contact developer support team. They will work with you through a hot-fix process.
Nikola
VC++ - I agree, product support is a great way to get QFE's. I think that if you open up a paid incident and it is deemed to be a bug then the incident is free (correct me if I'm wrong)
Just a quick reminder, you can also vote for it here:
http://lab.msdn.microsoft.com/productfeedback/viewFeedback.aspx?feedbackid=e08bd793-3fef-40ff-adda-ed313e0eafcc
it's already near the top of the overall top 10 list. Here's a temporary fix - use at own risk - note I've only provided a workaround for the non-DLL version of STL. The DLL version would be the same except you would have to change slightly different files (in CRT\SRC) and rebuild the the CRT (msvcp80.dll) - more complicated. Note: I based this fix on the way things worked in VC 2003.
Steps:
1) Open up istream in \Program Files\Microsoft Visual Studio 8\VC\Include
2) Before line 29, insert the following code
explicit __CLR_OR_THIS_CALL basic_istream(_Mysb *_Strbuf, bool _Isstd, bool _Noinit)
: _Chcount(0)
{ // construct from stream buffer pointer
if (!_Noinit)
_Myios::init(_Strbuf, _Isstd);
}
3) In the same file (istream), look for the following line:
: basic_istream<_Elem, _Traits>(_Strbuf, false),
and change it to:
: basic_istream<_Elem, _Traits>(_Strbuf, false, true),IMPORTANT NOTE: just after I posted my own workaround, the following official workaround was posted to the bug.
This one comes from PJ Plauger (author of STL) himself:
http://connect.microsoft.com/VisualStudio/feedback/Workaround.aspx?FeedbackID=98861I'd recommend using any official workaround before mine.
For rebuilding the DLLs, there are some modifications you would likely have to do (i.e. avoid having multiple versions of the CRT in memory, if using DLLs dependent on MSVCR80.DLL, e.g. MFC).
[edit: changed URL from old Feedback Center to Microsoft Connect]- Hi,
I can tell you that we are very aware of this bug. You may see a workaround posted for this issue on MSDN feedback site, http://lab.msdn.microsoft.com/ProductFeedback/ViewWorkaround.aspx?FeedbackID=FDBK40119#1. You may use it for now before official hotfix is available.
Nikola
VC++ status now shows as "fixed in the next version of Visual Studio".
Can you elaborate - will there be new redist binaries, msm files etc for msvcp80.dll anytime soon?
In this case, I think this bug is serious enought to be publicly available in the SP1 to VS2005.
But if your company requests a fix before SP1, you have to contact Developer Support team via support.microsoft.com and request a hotfix. In this case, the fix from our source will be packaged and delivered to your company.
Nikola
That's good news that a service pack will be made available. Any idea when SP1 is expected to be delivered?
Thanks.
The bug has been fixed. And a hotfix is in process of being release to companies who requested it. A fix is most likely going to be publicly available in VS2005 SP1.
Nikola
- 1º) In FDBK40119#1 the step:
3. Rebuild the MSVC libraries by running:
{C:\Program Files\Microsoft Visual Studio 8}\vc\crt\src\bldnt.cmd
* See reference link from description for details on rebuilding the MSVC libraries.
When i ran the command, multiple errors comes out, indicating that it needs the inclusion of the file windows.h. When i include the directory {C:\Program Files\Microsoft Visual Studio 8}\PlatformSDK\include for compiling, others errors appears like redefinition of types, etc. Where should i take the includes file that are missing in the crt directory?
2º) Exist VS2005 SP1? Not sure why you're having the problems, but rebuilding is ugly for several reasons, one of which is that you'll have to ship applocal and in every folder you require the CRT.
The better solution is to get a hotfix of the fixed redistributable directly from Product support services by phoning them (it will be a totally free support incident as it is a bug - ask for vcredist_x86.exe - they can search for you)
I am having trouble getting the hotfix from Product Support. They say they can't help me if I don't have a "Q article" number for them. Does anyone have this information?
Thanks,
Andrew Riffel
- The new KB id is 919280
Sorry, There seems no such hotfix for KB919280.
Untrue - I downloaded it a couple of days ago from Microsoft (147 megabytes) after contacting Microsoft PSS. It installed: new versions all the DLLs (8.0.50727.163) and all new MSM files, as well as updated CRT source code for the relevant fixes. Only the KB article hasn't been published, but they have the article internally within Microsoft. You just have to ask for the hotfix by KB number and they'll send you a password protected link to download the patch.
- Yes, Got it. Thank you.
It seams Visual C++ Express 2005 has the bug which is coherent.
Do you think there will be a service pack 1 which correct the bug ?
Do you think something will be published before the end of the year ?
With this problem, I have to go back to the Visual C++ ToolKit 2003 which seams to work fine.
Thank you in advance,
Etienne.HI,
Yes, the fix for this bug is include in SP1 patch to VC++2005, http://blogs.msdn.com/vcblog/articles/643313.aspx
SP1 is scheduled for Q3 of this year, http://msdn.microsoft.com/vstudio/support/servicing/sp1_vs05/default.aspx.
Thanks,
Nikola
- Is there a separate workaround for VC++ 2005 Express Edition?
The directory that the workaround refers to (..\vc\crt\..) doesn't seem to exist...
Workaround: http://connect.microsoft.com/VisualStudio/feedback/Workaround.aspx?FeedbackID=98861
Directory: {C:\Program Files\Microsoft Visual Studio 8}\vc\crt\src\ The workaround is basically rebuilding CRT and SCL library. Unfortunately it is not possible to do this with VC++ Express because CRT sources are not shipped with VC++ Express. You will have to wait till VS2005 SP1 to get fix for this issue.
Thanks,
Nikola
- We've received the hotfix, and it correctly addresses the memory leak. However; a new question arises: How do you redistribute the patched dll's contained in the hotfix?
Giles Biddison wrote: We've received the hotfix, and it correctly addresses the memory leak. However; a new question arises: How do you redistribute the patched dll's contained in the hotfix? Hotfix installs new versions of VC MSMs that contain new versions of DLLs. You may just merge these MSMs with a patch you are going to send out to your customers. Or if you are building a new version of a product, you can just use MSMs from hotfix instead of VS2005 versions. You should not have VS2005 versions of MSMs on your computer after you install hotfix because hotfix replaces files in place with the new copies. You can also check time stamp and version of files in MSMs to be sure.
Thanks,
Nikola
- Well this sucks. I just ran into this issue whilst testing my own class for memory leaks. I spent ages wondering where the leak was only to find out it was from the stringstream I was using. I'm using VC++ 2005 Express
So when is SP1 released? until SP1 is released, you can get hotfixes: 919280 (not sure if it works with Express). You can also get: 919588 for a hotfix of the CRT libraries
more info here:
http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=827852&SiteID=1&PageID=2
- SP1 was released to the public on Dec 15th 2006.
- Hello all:
Can anyone tell me why there is a memory link in the following code?
Thank you
-DanielMSDev23 wrote: This simple program seems to leak LOTS of memory when compiled in VC++ 2005:
int _tmain(int argc, _TCHAR* argv[])
{
for(int i = 0; i < 100000; ++i)
{
::Sleep(1);
std::basic_stringstream<TCHAR> str;
str << _T("Current iteration: ") << i;
}
return 0;
}
CRT: Multi-threaded debug or Multi-threaded
I compiled and ran the same piece of code in VC++ 2003 and I don't see ANY leaks!
Any ideas? Please help!
Thanks in advance I'm afraid this still seems to be present in VC++ 2008!
Any chance of a fix here too?
- I concur, I also had to deal with this memory leak within the stringstream with VC++ 2005 express in my case (recent download!). Has this (major) bug still not been fixed??
I had to switch to ostringstream as a solution.


