C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

delete the dynamic memory buffer ststream is mapped to

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Luke
Guest





PostPosted: Sat Dec 23, 2006 4:47 am    Post subject: delete the dynamic memory buffer ststream is mapped to Reply with quote



Hi all,

I know about the fact that strstream has been deprecated and that it is
difficult to manage safely (esp. the str() with a following mandatory
freeze(0)). Having to maintain a rather large portable library that has
been around for a few years, I discovered an issue, however, using the
HP C++ compiler version V7.2-018 on OpenVMS IA64 V8.3.
strstream, unlike stringstream, can easily be mapped to a memory buffer
- ok, deprecated, but handy and the general approach is that I need to
change existing code as little as possible while maintaining
portability. Following code works fine after compilation on Win32
(VC6/VC7/VC8), Linux and OpenVMS Alpha. It crashes however on IA64 (see
above for the exact configuration) when executing "delete os;". When I
comment out "delete rdbuf();" the code works fine again on all
platforms but I get a serious memory leak on all of them. Hence my
question: how can I properly clean up in a portable way?

int aSize = 4096;
ostream* os = new ostream(new strstreambuf(aSize));
*os << "Hello world";
// ...
os->rdbuf()->freeze(0);
delete rdbuf();
delete os;

Kindest regards,
Luc Vervoort


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Thomas Maeder
Guest





PostPosted: Sun Dec 24, 2006 1:14 am    Post subject: Re: delete the dynamic memory buffer ststream is mapped to Reply with quote



"Luke" <lcvervoort (AT) yahoo (DOT) com> writes:

Quote:
It crashes however on IA64 (see above for the exact configuration)
when executing "delete os;". When I comment out "delete rdbuf();"
the code works fine again on all platforms but I get a serious
memory leak on all of them. Hence my question: how can I properly
clean up in a portable way?

int aSize = 4096;
ostream* os = new ostream(new strstreambuf(aSize));

I'd start with

strstreambuf buf(aSize);
ostream os(&buf);

.. This will already eliminate two of the possible ways of leaking
memory.

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Frederic Lachasse
Guest





PostPosted: Sun Dec 24, 2006 1:16 am    Post subject: Re: delete the dynamic memory buffer ststream is mapped to Reply with quote



"Luke" <lcvervoort (AT) yahoo (DOT) com> wrote in message
news:1166802599.526964.140440 (AT) h40g2000cwb (DOT) googlegroups.com...
Quote:
Hi all,

I know about the fact that strstream has been deprecated and that it is
difficult to manage safely (esp. the str() with a following mandatory
freeze(0)). Having to maintain a rather large portable library that has
been around for a few years, I discovered an issue, however, using the
HP C++ compiler version V7.2-018 on OpenVMS IA64 V8.3.
strstream, unlike stringstream, can easily be mapped to a memory buffer
- ok, deprecated, but handy and the general approach is that I need to
change existing code as little as possible while maintaining
portability. Following code works fine after compilation on Win32
(VC6/VC7/VC8), Linux and OpenVMS Alpha. It crashes however on IA64 (see
above for the exact configuration) when executing "delete os;". When I
comment out "delete rdbuf();" the code works fine again on all
platforms but I get a serious memory leak on all of them. Hence my
question: how can I properly clean up in a portable way?

int aSize = 4096;
ostream* os = new ostream(new strstreambuf(aSize));
*os << "Hello world";
// ...
os->rdbuf()->freeze(0);
delete rdbuf();
delete os;

You should delete the streambuf *after* deleting the os. In your code, the
ostream does not know you have deleted its streambuf, so any maintenance
task it needs to do will have undefined behaviour (using a deleted object).

And AFAIK, the deprecated status of strstream has been removed in the recent
revision of the C++ standard, as people found out that in some cases,
strstream was more efficient.

--
Frédéric Lachasse - ECP86


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.