Topic: delete strstream str() buffers


Author: chris.carroll@dsw.govt.nz
Date: 1999/05/22
Raw View
I've recently read that the safest way to delete the memory used by a
strstream is to do the following:

myStrstream.rdbuf()->freeze(0)

This works perfectly fine for ostrstreams (and in fact I've been using
it for a while).  However, when I attempt to do the same on a istrstream
I get a core dump.  What am I doing wrong?  Do you not call these
methods on istrstreams?

Chris

Code Sample:

#include <strstream.h>
#include <string>

using namespace std;

int main(int argc, char* argv[]) {

string myString = "Hello";

istrstream istr(myString.c_str());

// This line causes a core dump.
istr.rdbuf()->freeze(0);

return 0;
}

This is what a debugger stack trace gives:

#0  0xc0086044 in free () from /usr/lib/libc.2
#1  0xc27873bc in operator delete () from /usr/lib/libCsup.2
#2  0xc27e66bc in strstreambuf::__dt () from /usr/lib/libstream.2
#3  0xc27e6bec in strstreambase::__dt () from /usr/lib/libstream.2
#4  0xc27e7270 in istrstream::__dt () from /usr/lib/libstream.2
#5  0x5090 in main () from istest


--== Sent via Deja.com http://www.deja.com/ ==--
---Share what you know. Learn what you don't.---
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: haberg.REMOVE@matematik.su.se (Hans Aberg)
Date: 1999/05/23
Raw View
In article <7ht4ah$4aa$1@nnrp1.deja.com>, chris.carroll@dsw.govt.nz wrote:

>I've recently read that the safest way to delete the memory used by a
>strstream is to do the following:
>
>myStrstream.rdbuf()->freeze(0)
>
>This works perfectly fine for ostrstreams (and in fact I've been using
>it for a while).  However, when I attempt to do the same on a istrstream
>I get a core dump.  What am I doing wrong?  Do you not call these
>methods on istrstreams?

Note that strstream's can be buggy as they are deprecated in the C++
standard (only currently supported for backwards compatibility). So
instead use stringstream in header <sstream>:
    string myString = "Hello";
    istringstream istr(myString);

On your program
>string myString = "Hello";
>istrstream istr(myString.c_str());
>// This line causes a core dump.
>istr.rdbuf()->freeze(0);

I am not sure why you are calling freeze(0) here, as the istrstream istr
should be created in dynamic memory allocation mode. If you take a poiter
to the buffer by str(), it will be frozen. But on the other hand, I do not
see why unfreezing an already unfrozen buffer should cause any problems.
-- Perhaps you should check with the compiler manufacturer if it is not a
compiler bug.

  Hans Aberg   * Anti-spam: Remove ".REMOVE" from email address.
               * Email: Hans Aberg <haberg.REMOVE@member.ams.org>
               * Home Page: <http://www.matematik.su.se/~haberg/>
               * AMS member listing: <http://www.ams.org/cml/>
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: James Kuyper <kuyper@wizard.net>
Date: 1999/05/23
Raw View
chris.carroll@dsw.govt.nz wrote:
>
> I've recently read that the safest way to delete the memory used by a
> strstream is to do the following:
>
> myStrstream.rdbuf()->freeze(0)

strstream is a deprecated feature, provided purely for backwards
compatibility, and unlikely to be in the next release of the standard.
I'd recommend not including it in new code unless you must. If you must
use it, you could still need to call freeze(0), but only if some other
part of your program (or a library that you link to) calls freeze(1).
I've no idea why you're getting a core dump, however.
The preferred alternative is stringstream. I haven't found anything
equivalent to freeze() in basic_stringbuf<>, so I doubt that it would be
needed, but I'm still learning the strings/iostream part of the library.
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: hinnant@_anti-spam_metrowerks.com (Howard Hinnant)
Date: 1999/05/23
Raw View
In article <7ht4ah$4aa$1@nnrp1.deja.com>, chris.carroll@dsw.govt.nz wrote:

> I've recently read that the safest way to delete the memory used by a
> strstream is to do the following:
>
> myStrstream.rdbuf()->freeze(0)
>
> This works perfectly fine for ostrstreams (and in fact I've been using
> it for a while).  However, when I attempt to do the same on a istrstream
> I get a core dump.  What am I doing wrong?  Do you not call these
> methods on istrstreams?
>
> Chris
>
> Code Sample:
>
> #include <strstream.h>
> #include <string>
>
> using namespace std;
>
> int main(int argc, char* argv[]) {
>
> string myString = "Hello";
>
> istrstream istr(myString.c_str());
>
> // This line causes a core dump.
> istr.rdbuf()->freeze(0);
>
> return 0;
> }
>
> This is what a debugger stack trace gives:
>
> #0  0xc0086044 in free () from /usr/lib/libc.2
> #1  0xc27873bc in operator delete () from /usr/lib/libCsup.2
> #2  0xc27e66bc in strstreambuf::__dt () from /usr/lib/libstream.2
> #3  0xc27e6bec in strstreambase::__dt () from /usr/lib/libstream.2
> #4  0xc27e7270 in istrstream::__dt () from /usr/lib/libstream.2
> #5  0x5090 in main () from istest

This looks like a "joint effort" problem to me.

1.  Bug in your code:  You can only give istrstream responsibility for a
buffer that it initially allocated.  In this case, the buffer you are
attempting to give it responsibility for is owned by myString.

2.  Bug in lib: strstreambuf is supposed to guard against this kind of
programmer error.  Your call to freeze should have had no effect.  But it
looks like it did indeed give the strstreambuf ownership of the buffer.
Now this memory gets deleted first when the istr destructor runs, and then
again when the myString destructor runs (resulting in your crash).

-Howard
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]