VC++ 2005的STL的内存泄露
2005 CRT memory leaks: std::basic_iostream ( affects std::stringstream, std::fstream, probably others )!!!
这真是件让人郁闷的事情!下面的代码将重现这个错误:
2
3 int main()
4 {
5 unsigned int x = 10000000;
6 while( x-- )
7 {
8 std::iostream s(0);
9 }
10 }
每次泄漏是4Bytes, 结果可想而知!当然,这么重要的错误,MS不可能没有反应。FDBK40119#1: Patch ostream and istream and rebuild msvcp80.dll (http://lab.msdn.microsoft.com/ProductFeedback/ViewWorkaround.aspx?FeedbackID=FDBK40119#1) 有说明解决方案,不过由于要手动操作,给人的感觉并不好。不过,不手动操作又无法解决这个问题,因为:
We have plans for two service pack releases for the first part of 2006:
- VS 2003 SP1 is scheduled for April 2006. We have done much of the work for this release already, and are anxious to get it to you.
- VS 2005 SP1 is scheduled for the first half of 2006. We will be more specific about the date in a few months, once we have more customer data.
(http://blogs.msdn.com/scottwil/archive/2005/11/07/490007.aspx)
-------------------
手动操作的说明:
References:
P.J. Plauger, Dinkumware - Supplied the modified constructors.
"Building the Run-Time Libraries, MSDN2 online, http://msdn2.microsoft.com/en-us/library/k9a8ehy3.aspx - How to rebuild the MSVC runtime libraries.
Workaround Steps:
1. Change the relevant constructor in
{C:/Program Files/Microsoft Visual Studio 8}/vc/include/istream and
{C:/Program Files/Microsoft Visual Studio 8}/vc/crt/src/istream
* directory between {} is the location of the MSVC 8 install directory
explicit __CLR_OR_THIS_CALL basic_iostream(basic_streambuf<_Elem,_Traits> *_Strbuf)
: basic_istream<_Elem, _Traits>(_Strbuf, false),
basic_ostream<_Elem, _Traits>(_Noinit, false)
{ // construct from stream buffer pointer
}
注:修改前内容
explicit __CLR_OR_THIS_CALL basic_iostream(basic_streambuf<_Elem, _Traits> *_Strbuf)
: basic_istream<_Elem, _Traits>(_Strbuf, false),
basic_ostream<_Elem, _Traits>(_Strbuf)
{ // construct from stream buffer pointer
}
2. Change the relevant constructor in
{C:/Program Files/Microsoft Visual Studio 8}/vc/include/ostream and
{C:/Program Files/Microsoft Visual Studio 8}/vc/crt/src/ostream
__CLR_OR_THIS_CALL basic_ostream(_Uninitialized, bool _Addit = true)
{ // construct uninitialized
if (_Addit)
ios_base::_Addstd(this); // suppress for
iostream
}
注修改前内容:
__CLR_OR_THIS_CALL basic_ostream(_Uninitialized)
{ // construct uninitialized
ios_base::_Addstd(this);
}
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.
注:注意使用前需要设置一个环境变量,makefile文件中有相关说明
# If your MSVC++ 8.0 installation is not in the default installation path
# of "/Program Files/Microsoft Visual Studio 8/VC" on the current drive,
# set the environment variable VCTOOLS to point to the main directory
# of your installation. (For example, "set VCTOOLS=C:/VS.NET/VC8")
4. Modify the Visual Studio project files
a. Properties->Linker->Input , "Ignore Specific Libraries" : msvcprt.lib;msvcrt.lib
b. Properties->Linker->Input, "Additional Dependencies" :
"{C:/Program Files/Microsoft Visual Studio 8}/vc/crt/src/build/intel/sample_p.lib"
"{C:/Program Files/Microsoft Visual Studio 8}/vc/crt/src/build/intel/_sample_.lib"
* Refer to reference link from description on details of the file naming.
5. Put the sample_p.dll and _sample_.dll in the same directory as your app ( or follow the rules for side-by-side deployment).
转载自:http://www.cnblogs.com/WuErPIng/archive/2006/01/01/309514.html
作者:吴尔平