Crash in dynamic cast in dll code

Discussion in 'C++' started by Peeyush81@gmail.com, Oct 25, 2007.

  1. Guest

    Hi All,
    I am trying to invoke methods in c++ code in two ways, one by
    compiling the calling code with the called code and running it
    standalone, other by making a dll of the called code and then calling
    it. First case works fine but in the 2nd case, I encounter a crash
    at the following line
    CThread* ct = &CThread::GetCurrentThread();
    TEventHandler* eh = dynamic_cast< TEventHandler* >( ct );

    Any hints why the crash occurs?

    Thanks,
    Peeyush
    , Oct 25, 2007
    #1
    1. Advertising

  2. Re: Crash in dynamic cast in dll code

    On Oct 25, 11:23 am, wrote:
    > Hi All,
    > I am trying to invoke methods in c++ code in two ways, one by
    > compiling the calling code with the called code and running it
    > standalone, other by making a dll of the called code and then calling
    > it. First case works fine but in the 2nd case, I encounter a crash
    > at the following line
    > CThread* ct = &CThread::GetCurrentThread();
    > TEventHandler* eh = dynamic_cast< TEventHandler* >( ct );
    >
    > Any hints why the crash occurs?
    >

    The most obvious thing is that you're dynamic casting to a pointer. If
    that fails it returns NULL. Do you test eh for being NULL as you
    always should with bare pointers?
    That's the most obvious culprit, to me at least. What's the difference
    in the code that's doing this in the local code and in the DLL? Can a
    CThread* be safely cast to a TEventHandler*.
    Jonathan Lane, Oct 25, 2007
    #2
    1. Advertising

  3. Guest

    Re: Crash in dynamic cast in dll code

    Make sure that you compile the DLL with RTTI support. otherwise there
    is no
    RTTI info to support the dynamic_cast and then it will crash.



    On 10 25 , 6 23 , wrote:
    > Hi All,
    > I am trying to invoke methods in c++ code in two ways, one by
    > compiling the calling code with the called code and running it
    > standalone, other by making a dll of the called code and then calling
    > it. First case works fine but in the 2nd case, I encounter a crash
    > at the following line
    > CThread* ct = &CThread::GetCurrentThread();
    > TEventHandler* eh = dynamic_cast< TEventHandler* >( ct );
    >
    > Any hints why the crash occurs?
    >
    > Thanks,
    > Peeyush
    , Oct 25, 2007
    #3
  4. Guest

    Re: Crash in dynamic cast in dll code

    Thanks for the prompt responses,
    Jonathan,
    The pointer is not null and assigned a valid address.( I checked in
    debug)
    I cannot find any differences between the two ways of invoking the
    code.

    Liao,
    I have RTTI check enabled now. Now it does not crash at that line but
    at the line next to it.
    If I continue ,it repeats this arbitrary behavior at random lines,
    even at
    simple String assignments.

    TString fddevName = "DAL";

    Could it be a problem of stack
    overflow bacause of insufficient memory to the program when running in
    dll mode?
    , Oct 26, 2007
    #4
  5. Guest

    Re: Crash in dynamic cast in dll code

    Environment Specs-
    Win XP, VC++ 6.0
    , Oct 26, 2007
    #5
  6. Ron Natalie Guest

    Re: Crash in dynamic cast in dll code

    wrote:

    > even at
    > simple String assignments.
    >
    > TString fddevName = "DAL";
    >
    > Could it be a problem of stack
    > overflow bacause of insufficient memory to the program when running in
    > dll mode?
    >

    What the * is a TString? "DAL" is not a string, it's an array
    of char[].

    The other Visual Studio gotcha is to make sure all your DLL's are
    compiled with the same C++ runtimes (see the CODE GENERATION TAB)
    or to be darned sure that you do all your memory allocation and
    deallocation in the same DLL (i.e., no inlined allocations in the
    headers!)>

    Your best bet is to take this off-topic qustion over to one
    of the groups that has "microsoft" in its name.
    Ron Natalie, Oct 26, 2007
    #6
  7. Re: Crash in dynamic cast in dll code

    On Oct 26, 5:41 am, wrote:
    > Thanks for the prompt responses,
    > Jonathan,
    > The pointer is not null and assigned a valid address.( I checked in
    > debug)
    > I cannot find any differences between the two ways of invoking the
    > code.
    >
    > Liao,
    > I have RTTI check enabled now. Now it does not crash at that line but
    > at the line next to it.
    > If I continue ,it repeats this arbitrary behavior at random lines,
    > even at
    > simple String assignments.
    >
    > TString fddevName = "DAL";
    >
    > Could it be a problem of stack
    > overflow bacause of insufficient memory to the program when running in
    > dll mode?


    I would doubt it's run out of memory to be honest. If it's crashing at
    a random point later then you've obviously already corrupted the heap.
    I did find, a while back, a technique to log all allocations and de-
    allocations. Try googling for something like that to see where you're
    corrupting the heap.

    Otherwise, as Ron Natalie suggests it could be down to using different
    runtimes in the different components. Check that in the project
    settings. Also make sure that you're linking against the same version
    of the libraries that supply these types. I've seen errors similar to
    this where different versions of the STL are used by the third party
    vendor and STL types are passed on the command line.

    Failing that, the types you have are all MS types so try a MS C++
    newsgroup.
    Jonathan Lane, Oct 26, 2007
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Anushi
    Replies:
    5
    Views:
    8,590
  2. MSG

    to cast or not to cast malloc ?

    MSG, Feb 6, 2004, in forum: C Programming
    Replies:
    38
    Views:
    1,048
    Dan Pop
    Feb 10, 2004
  3. Gernot Frisch

    strange crash - cast short* to int*

    Gernot Frisch, Oct 15, 2008, in forum: C++
    Replies:
    5
    Views:
    412
    Kai-Uwe Bux
    Oct 16, 2008
  4. Pavel
    Replies:
    7
    Views:
    503
    Pavel
    Sep 19, 2010
  5. H. Simpson
    Replies:
    4
    Views:
    236
    H. Simpson
    Aug 3, 2004
Loading...

Share This Page