Visual C++ Developer Center > Visual C++ Forums > Visual C++ Language > stringstream causes memory link VC++ 2005?
Ask a questionAsk a question
 

Answerstringstream causes memory link VC++ 2005?

  • Wednesday, November 09, 2005 2:44 PMMSDev23 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    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

  • Wednesday, November 09, 2005 4:01 PMmike_n Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    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...


  • Wednesday, November 09, 2005 5:13 PMAyman Shoukry - MSFTModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    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
  • Tuesday, December 06, 2005 1:47 PMTed_ Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer

    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=98861

    I'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]

  • Tuesday, December 06, 2005 11:50 PMNikola Dudar - MSFT _old name_ Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    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++
  • Friday, February 24, 2006 9:54 PMNikola Dudar - MSFT _old name_ Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer

    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

  • Monday, June 12, 2006 8:56 PMTed_ Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    The new KB id is 919280

All Replies

  • Wednesday, November 09, 2005 4:01 PMmike_n Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    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...


  • Wednesday, November 09, 2005 5:13 PMAyman Shoukry - MSFTModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    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
  • Wednesday, November 09, 2005 7:39 PMMSDev23 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    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.
  • Wednesday, November 09, 2005 7:41 PMAyman Shoukry - MSFTModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    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

  • Wednesday, November 09, 2005 7:45 PMMSDev23 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    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!
  • Thursday, November 10, 2005 7:01 PMMSDev23 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    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.
  • Thursday, November 10, 2005 9:02 PMAyman Shoukry - MSFTModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    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
  • Sunday, November 13, 2005 6:35 AMMadhu74 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    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.
  • Sunday, November 13, 2005 7:43 AMAyman Shoukry - MSFTModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    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

  • Sunday, November 13, 2005 11:50 PMMadhu74 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Thanks Ayman.. I have already voted using the link. How do I contact PSS to request a private fix?

    Thanks,
    Madhu.
  • Monday, November 14, 2005 11:28 PMAyman Shoukry - MSFTModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    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
  • Thursday, November 17, 2005 2:19 AMMadhu74 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    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.
  • Thursday, November 17, 2005 4:38 AMAyman Shoukry - MSFTModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    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

  • Thursday, December 01, 2005 4:11 PMTed_ Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    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.
  • Thursday, December 01, 2005 5:21 PMMadhu74 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    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..Tongue TiedTongue TiedTongue Tied

    Thanks,
    Madhu.

  • Thursday, December 01, 2005 5:32 PMBrian KramerModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    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

  • Thursday, December 01, 2005 5:48 PMAyman Shoukry - MSFTModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    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
  • Thursday, December 01, 2005 6:18 PMNikola Dudar - MSFT _old name_ Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    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++
  • Thursday, December 01, 2005 6:58 PMTed_ Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    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.
  • Monday, December 05, 2005 8:46 PMTed_ Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    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),

  • Tuesday, December 06, 2005 1:47 PMTed_ Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer

    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=98861

    I'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]

  • Tuesday, December 06, 2005 11:50 PMNikola Dudar - MSFT _old name_ Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    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++
  • Tuesday, January 24, 2006 5:51 PMTed_ Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    http://lab.msdn.microsoft.com/productfeedback/viewFeedback.aspx?feedbackid=e08bd793-3fef-40ff-adda-ed313e0eafcc

    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?

  • Saturday, February 04, 2006 1:56 AMNikola Dudar - MSFT _old name_ Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    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

  • Saturday, February 04, 2006 4:04 PMMSDev23 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    That's good news that a service pack will be made available.  Any idea when SP1 is expected to be delivered?

     

    Thanks.

  • Friday, February 24, 2006 9:54 PMNikola Dudar - MSFT _old name_ Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer

    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

  • Wednesday, April 26, 2006 11:38 PMSeso Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    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?
  • Thursday, April 27, 2006 12:10 PMTed_ Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    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)

  • Monday, June 12, 2006 5:46 PMAndyRiffel Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    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

  • Monday, June 12, 2006 8:56 PMTed_ Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    The new KB id is 919280
  • Friday, June 16, 2006 1:20 AMBrian TM Chen Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

     

     Sorry, There seems no such hotfix for KB919280.

  • Friday, June 16, 2006 4:23 AMTed_ Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    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.

  • Wednesday, June 21, 2006 10:23 AMBrian TM Chen Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Yes, Got it. Thank you.
  • Monday, July 17, 2006 9:37 AMEgrignon Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    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.

  • Monday, July 17, 2006 4:27 PMNikola Dudar - MSFT _old name_ Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    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

  • Wednesday, July 26, 2006 7:53 PMFixion Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    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\



  • Thursday, July 27, 2006 2:15 AMNikola Dudar - MSFT _old name_ Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    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

  • Thursday, August 03, 2006 12:30 AMGiles Biddison Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    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?
  • Thursday, August 03, 2006 12:37 AMNikola Dudar - MSFT _old name_ Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

     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

  • Wednesday, November 08, 2006 12:32 PMMike Weller Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    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?
  • Thursday, November 09, 2006 1:11 PMTed_ Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    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

  • Monday, January 15, 2007 3:50 PMMikWells Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    SP1 was released to the public on Dec 15th 2006.

  • Tuesday, January 16, 2007 5:52 AMdaniel mark Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hello all:

    Can anyone tell me why there is a memory link in the following code?

    Thank you
    -Daniel


     MSDev23 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

  • Wednesday, November 28, 2007 3:14 PMmartastik Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    I'm afraid this still seems to be present in VC++ 2008!

     

    Any chance of a fix here too?

  • Thursday, July 23, 2009 10:07 PMgobber_1 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    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.