There's something wrong with the ios_base class in Visual Studio 2005. I was debugging my program and I noticed a memory leak. Since I could not understand what was causing that leak, I've tried using a memory leak detection tool (Rational Purify) and I found that the ios_base class causes a memory leak.
I just tried to write this piece of code
#include <sstream>
int main(void)
{
std::stringstream myStream;
//nothing else
return 0;
}
The memory leak detection program tells me that:
--Memory leak of 4 bytes from 1 block allocated in
--std::basic_iostream<char,char_traits<char>::std>::basic_iostream<char,char_traits<char>::std>(basic_streambuf<char,char_traits<char>::std>::std*)
--at file xiosbase
--at method std::ios_base::_Init(void)
--at line _Ploc = _NEW_CRT locale;
Is this true or I am misinterpreting the program's output
Please help me!
Thanks

Memory Leak in C++ STL library - Visual Studio 2005
aus82
const char* fname = "C:\\test.txt";
for(int i=0; i<999999999; i++)
{
fstream binary_file(fname,ios:
binary_file.close();
}
compared to:
const char* fname = "C:\\test.txt";
for(int i=0; i<999999999; i++)
{
ofstream binary_file(fname,ios:
binary_file.close();
}
it is not even neccessary to write anything to the file to get the memory leak in the first case
Priya RV
See OShahs post. There's a pretty extensive thread on the subject, as well as a patch available upon request.
A.Russell
brent.xml
David Pallmann
I also found the memory leak bug. It is caused by strange initialization in std::ios_base, where _Ploc is not initialized in std::ios_base's constructor but in _Init method, which is called twice in std::basic_iostream initialization: once for istream and once for ostream, and the second one replaces _Ploc pointer without deleting previously allocated locale object. I've solved it by rewriting ios_base constructor and _Init method (xiosbase, line ~500):
__CLR_OR_THIS_CALL ios_base(): _Ploc(0)
...
void __CLR_OR_THIS_CALL _Init()
{ // initialize a new ios_base
if (_Ploc)
_DELETE_CRT(_Ploc);
...
But it works only if static runtime libraries are used.
Is there any chance that Microsoft (or Dimkunware) fix it short time The std::basic_iostream class is used heavily (through boost::lexical_cast) in my programs and the memory leaks are significant!
JohnRobbins
Yeah, we know.
In the meantime, try to minimise your use of the i&o streambufs. Make use of the i or o variants of the streambufs (ofstream, ifstream, istringstream, ostringstream, but not fstream/stringstream) as much as possible.