Strstreambuf memory leak

R

Rajwarya

Hi ppl,

I am trying to run a code on SUN OS 5.8 and using Sun WorkShop 6
update 2 C++ 5.3. The problem is that the code is leaking memory in
strstreambuf. We are using ostrstream with rdbuf()->freeze(0) but
still its bleading. I am pasting the purify output below. Please let
me know how to get rid of this.
Also we are using latest patch release for libCstd.so.1 i.e Sun C++
5.6 Patch 108434-17 DEV

MLK: 1430016 bytes leaked in 11172 blocks
This memory was allocated from:
malloc [rtlib.o]
c2n6Fi_Pv___1 [libCrun.so.1]
void*operator new(unsigned) [rtlib.o]
void*operator new[](unsigned) [rtlib.o]
int std::strstreambuf::eek:verflow(int) [libCstd.so.1]
long std::strstreambuf::xsputn(const char*,long)
[libCstd.so.1]
Block of 128 bytes (11172 times); last block at 0x1675f1d8
MLK: 633700 bytes leaked in 7034 blocks
MLK: 20480 bytes leaked in 160 blocks
This memory was allocated from:
malloc [rtlib.o]
c2n6Fi_Pv___1 [libCrun.so.1]
void*operator new(unsigned) [rtlib.o]
void*operator new[](unsigned) [rtlib.o]
int std::strstreambuf::eek:verflow(int) [libCstd.so.1]
void __rwstd::digit_writer said:
::put_digits(char) [libCstd.so.1]
Block of 128 bytes (160 times); last block at 0x1675f100


Thanks
Rajat
 
A

Ali Cehreli

I am trying to run a code on SUN OS 5.8 and using Sun WorkShop 6 update
2 C++ 5.3. The problem is that the code is leaking memory in
strstreambuf. We are using ostrstream with rdbuf()->freeze(0) but still
its bleading.

Have you also ensured that the call to freeze is always executed?
Could there be an exception thrown before getting to that line?

Ali
 
R

Rajwarya

Hi Ali,

Thanks for the reply.
No the line rdbuf->freeze(0) is getting excecuted. Well the purify
output shows a reduction in memory leak. But it still there are other
leaks in strstreambuf to be plugged. Apart from ostrstream classes
there are other classes also that are bleading I guess. Can you tell
me those classes and how to seal them.
I am pasting purify outputs below .

Thanks
Rajat

Purify output after using rdbuf->freeze(0).
Memory leaked: 2099446 bytes (76.7%); potentially leaked: 1536 bytes
(0.0561%)
void*operator new(unsigned) [rtlib.o]
void*operator new[](unsigned) [rtlib.o]
int std::strstreambuf::eek:verflow(int) [libCstd.so.1]
long std::strstreambuf::xsputn(const char*,long)
[libCstd.so.1]

void*operator new(unsigned) [rtlib.o]
void*operator new[](unsigned) [rtlib.o]
int std::strstreambuf::eek:verflow(int) [libCstd.so.1]
void __rwstd::digit_writer said:
::put_digits(char) [libCstd.so.1]


Purify output without using rdbuf->freeze(0)
Memory leaked: 6648648 bytes (92.5%); potentially leaked: 23323 bytes
(0.324%)

c2n6Fi_Pv___1 [libCrun.a]
void*operator new(unsigned) [rtlib.o]
void*operator new[](unsigned) [rtlib.o]
int std::strstreambuf::eek:verflow(int) [libCstd.so.1]
long std::strstreambuf::xsputn(const char*,long)
[libCstd.so.1]

c2n6Fi_Pv___1 [libCrun.a]
void*operator new(unsigned) [rtlib.o]
void*operator new[](unsigned) [rtlib.o]
int std::strstreambuf::eek:verflow(int) [libCstd.so.1]
long std::strstreambuf::xsputn(const char*,long)
[libCstd.so.1]
 
A

Ali Cehreli

No the line rdbuf->freeze(0) is getting excecuted.

I will be very picky here and say that you can't be sure that it's
called every time it is needed.

#include <strstream>

void bar()
{
/* ... */
}

int main()
{
ostrstream out;
out << 42;
out.str();
bar(); // If bar throws, the following line is not executed
out.freeze(false);

out.str(); // BUG: need to call freeze again; but forgot!
}

At a previous job, I caught more than 50 memory leaks related to the
use of ostrstream, just by grepping for 'ostrstream' and examining
code. Most of them were related to missing 'freeze' calls.

I replaced all of them with ostringstreams but then some operations
became very slow: 10 seconds instead of 1 second! :(

Also, I am not sure whether you are supposed to call freeze on rdbuf
or ostrstream.

If Purify's output is not helpful enough try other tools like the free
valgrind on Linux.

Ali
 
M

Martin Sebor

....
But it still there are other
leaks in strstreambuf to be plugged. Apart from ostrstream classes
there are other classes also that are bleading I guess. Can you tell
me those classes and how to seal them.

The C++ Standard Library implementation that SunPro uses by default caches
a bunch of data in locale, iostreams, and even some containers (e.g., list).
Unfortunately, the library never releases the caches which shows up as memory
leaks in Purify. Your best option is to complain to Sun and nag them to update
to a later version of the library. Or buy the latest version of the library
from Rogue Wave :) It has none of these problems.

Martin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

strstream Memory Leak 5
Possible memory leak? 11
memory leak 4
memory leak with deque ? 4
valgrind/memory leak question 1
pthread memory leaks 15
memory manager to prevent memory leaks 4
troubles with memory leak 4

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top