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 ...

+ Reply to Thread
Results 1 to 3 of 3

Thread: passing std::cout as a parameter to a DLL crashes

  1. 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. But i've also
    set up the program ANOTHER time, using VC++6.0
    standard projects.

    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.


  2. Re: passing std::cout as a parameter to a DLL crashes

    Hello,

    The following might help:

    1. Remove /ML compilation switch (for both projects);
    2. For DEBUG build add /MDd (for both projects). For RELEASE build add /MD
    (for both projects);

    This modification will cause single instance of C-runtime library (DLL) used
    for both projects. This is required because you pass ref to CRT-based object
    (it's C++ streams, I know; but they still call C-runtime library rather than
    WIN32 api directly).

    This should probably solve the problem.

    Boris

    "Torsten Mohr" wrote in message
    news:bnbu7g$1vj0$1@news.lf.net...
    > 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. But i've also
    > set up the program ANOTHER time, using VC++6.0
    > standard projects.
    >
    > 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.
    >




  3. Re: passing std::cout as a parameter to a DLL crashes

    Hi,

    thanks,

    that was exactly the right thing to do.

    Now my code works just fine.


    Best regards,
    Torsten.


+ Reply to Thread