passing std::cout as a parameter to a DLL crashes - Programmer
This is a discussion on passing std::cout as a parameter to a DLL crashes - Programmer ; Hi,
i wrote a DLL that contains a class "aClass". That
class contains a method:
void aClass::show(std: stream& os) {
os val
// val is an "int". other types also crash
}
When i call this and pass std::cout as ...
-
passing std::cout as a parameter to a DLL crashes
Hi,
i wrote a DLL that contains a class "aClass". That
class contains a method:
void aClass::show(std:
stream& os) {
os << "val is " << this->val << std::endl;
// val is an "int". other types also crash
}
When i call this and pass std::cout as a parameter,
i can see the output "val is " and then
"abnormal program termination".
If i only output constant text, everything is fine.
If i use std::cout in the method, everything is fine.
I stripped the whole problem down to a test project
that contains no other code, i can't see what else
could cause this behaviour.
I called this project "cot" for C Out Test.
I compile everything with a Makefile.
Maybe the options i pass to "cl" or "link" could
be the problem?
Compile DLL
/nologo /O2 /ML /W3 /GX /D "WIN32" /D "NDEBUG" /D "_WINDOWS"
/D "_MBCS" /D "_USRDLL" /D "COT_EXPORTS" /D "_CONSOLE"
Compile test program
/nologo /O2 /ML /W3 /GX /D "WIN32" /D "NDEBUG" /D "_CONSOLE"
/D "_MBCS"
Link DLL
/nologo /subsystem:console /incremental:no /pdb:NONE
/machine:I386 /out:ctest.exe
Link test program
/nologo /dll /incremental:yes /pdb:cot.pdb /debug
/machine:I386 /out:cot.dll /implib:cot.lib /pdbtype:sept
Thanks for any hints,
Torsten.
-
Re: passing std::cout, _THROW(bad_cast, "missing locale facet");
Hi,
i rewrote the whole test program AGAIN, it still fails.
Now i've used the standard MS VC++ IDE. I wrote two
projects, beginning with an empty DLL project and a standard
Console project. The failure is still reproducible.
When i debug the code now, i get to a point where it says:
_THROW(bad_cast, "missing locale facet");
What's wrong with my code?
Can't i just pass std::cout to std:
stream& ? There
were no warnings about bad casts or anything like that.
Thanks for any hints,
Torsten.
-
Re: passing std::cout, _THROW(bad_cast, "missing locale facet");
Hi,
i just tried to add a locale to std::cout this way:
#include
....
std::locale* loc;
....
int main(...
loc = new std::locale("");
std::cout.imbue(*loc);
Now the Exception is not thrown any more, but in the member
function of the class to that i pass std::cout as a parameter,
it seems that the output is truncated:
os << "val is " << val << std::endl;
shows only:
Constructor
val is Destructor
Any hints?
Best regards,
Torsten.