1

I have a DLL streaming text to std::cerr. The EXE linking (via LoadLibrary()) to that DLL needs to get the text being streamed.

I am using VS90.

I use the following class in the EXE for the redirection:

struct cerr_redirect {
cerr_redirect( std::streambuf * new_buffer ) 
    : old( std::cerr.rdbuf( new_buffer ) )
{ }

~cerr_redirect( ) {
    std::cerr.rdbuf( old );
}

private:
std::streambuf * old;
};

This works fine as long as both DLL and EXE are build using the same configuration (either both Debug or both Release). However, when running a Release built EXE with a Debug built DLL, the redirection does not work and none of the cerr output made in the DLL gets to the EXE.

EDIT

The Runtime Library is linked dynamically. The Debug built DLL links against "Multi-threaded Debug DLL (/MDd)" while the Release built EXE links against "Multi-threaded DLL (/MD)".

I suspect this is the reason for this behavior; I believe std::cerr lives in the CRT. So when both DLL and EXE use the same CRT they share the same std::cerr, otherwise, they each have their own copy.

Could someone confirm if my understanding is correct?

If so, I think a solution to my problem is to define my own global stream in the EXE and let the DLL redirect cerr to it. Or is there a more elegant solution?

JPh
  • 536
  • 3
  • 20
  • 2
    When you mix release and debug builds this means that the executable and the DLL use different runtimes. And with two different runtimes, there are two different instances of `std::cerr`. The simple way out of this bind is to stop mixing release and debug builds. – David Heffernan Sep 08 '14 at 17:03
  • related: [Mixing release dlls with debug main application exe](http://stackoverflow.com/a/12290565/33499) and [Linking against library in release and .exe in debug crashes in Visual studio](http://stackoverflow.com/q/1227653/33499) – wimh Sep 08 '14 at 17:04
  • Thanks for the comments. Problem understood. – JPh Sep 09 '14 at 10:41

0 Answers0