Re: std::iostream/std::cout - Do not compile with vc-8_0
First off, a boost::shared_pointer will delete its pointee by default.
You can overcome that by passing in a null deleter to the shared
pointer constructor (details in the boost::shared_ptr docs). With what
you have, the shared pointer will call delete on &cout when the last
shared pointer is destructed (at the end of the program). This will
almost certainly cause a crash of some sort.
Yes obviously, I forget that but that is obvious.
Here is the code, that produce the same result.
// snippet on
#include <list>
#include <string>
#include <iostream>
#include <boost/shared_ptr.hpp>
struct null_deleter { void operator()(void const *) const {} };
int main()
{
std::list<boost::shared_ptr<std::ostream> > l;
boost::shared_ptr<std::ostream> p(&std::cout, null_deleter());
*p << "on the shared pointer" << std::endl;
l.push_back(p);
std::list<boost::shared_ptr<std::ostream> >::iterator it =
l.begin();
std::cout << "before loop" << std::endl;
while (it++ != l.end())
{
boost::shared_ptr<std::ostream> p2 = *it;
*p2 << "test" << std::endl;
}
return 0;
}
// snippet off
I'm not sure why you're getting a segfault before "test" is output,
since std::endl is supposed to flush buffers (I believe). If I'm
wrong, your program could be crashing at program termination due to
what I said above, but before all the buffers are flushed (certainly,
if you had used <<"\n" instead of <<std::endl, thats what I would have
guessed).
Actually it really crashes on the streaming. I debugged it with gdb.
It crashes on:
std::ostream::sentry::sentry () from /usr/lib/libstdc++.so.6
I'm not sure that what you are doing is necessarily the best way to
achieve what you want, but as I have no ther suggestions, try using a
null deleter, and report back.
What I am trying to do is to have a pool of streams where to write on.
The user add streams to the list, pass this to my library, and then I
shall manage deallocation. I don't want the user to mess around with
shared_ptr and I dont want him to allocate pointer either (RIIA, RAAI
or RIAA, I don't remember the term :) ...)
Thanks for your help.
JD