Hi all,
I have come across a situation (on Win32) where the std::ostringstream object continues to consume process memory, even when it is ostensibly cleared out after a series of append-type operations. Please take a look at this C++ fragment:
int main(void)
{
std::ostringstream cOutputLogStream;
// Random long string
std::string sTest = "jkspoiauyeraspfoiusdfsdfekgpweojkgpwoekpokgkpgeopoegwj";
std::string sEmpty = "";
int n = 0;
int looper = 0;
while (n++ < 100000)
{
while (looper++ < 45)
{
cOutputLogStream << s;
}
cOutputLogStream.str(sEmpty);
cOutputLogStream.clear();
// This should give the heap manager a chance to consolidate
// fragmented memory blocks
Sleep(1);
}
}
During execution of the inner while() loop, observation of the process's memory usage in Task Manager shows a continuous upward ramp, which eventually levels off. However, this levelling-off happens at the same time as when the error std::bad_alloc is repeatedly thrown. This would suggest that heap memory has either been exhausted, or the requested size of block is not available in a contiguous space.
Has anyone else experienced this leaking phenomenon with ostringstream objects, and what other alternative objects are available instead of this flaky one?
Many thanks!