I'm trying to grab all the input sent to cout to modify and redirect it. I used the example from http://www.dreamincode.net/code/snippet2499.htm, which works fine using the following code:

std::ofstream file_sink("encrypted.bin", std::ios_base::binary | std::ios_base::out);
    basic_xor_filter<char> filter(*(file_sink.rdbuf()), 0x7F);
    std::ostream output_stream(&filter);
    output_stream << "Hello World" << std::endl;

The class overrides streambuf and simply XORs the input characters before sending them to the provided streambuf (in this case, a file). My problem occurs when I try setting cout's rdbuf() to filter:

streambuf* s = &filter;
    cout.rdbuf( s );
    cout << "Hello World\n";

This code causes a glibc "double free or corruption (!prev)" error. Could anyone tell me where I am going wrong? Perhaps I just don't understand the concepts properly?

Thanks very much.

streambuf* s = &filter;
        streambuf* old = cout.rdbuf(s);
        cout << "Hello World" << endl;

How does your 'filter' look? Maybe more problems here!?