C++ application which crashes in dynamic_cast

Discussion in 'C++' started by yinglcs@gmail.com, Jan 17, 2007.

  1. Guest

    Hi,

    I have a c++ application which crashes in this line (from the debugger,
    I have a segmentation fault here):
    void *object = dynamic_cast<void>(aObject);


    I have stepped thru the code in debugger and the 'aObject' pointer is
    Not null. So can you please tell me, what else can go wrong so that my
    program crashes in this line?

    Thank you. for any help.



    Code:

    NS_IMETHODIMP
    nsTraceRefcntImpl::LogAddCOMPtr(void* aCOMPtr,
    nsISupports* aObject)
    {
    #if defined(NS_BUILD_REFCNT_LOGGING) &&
    defined(HAVE_CPP_DYNAMIC_CAST_TO_VOID_PTR)
    // Get the most-derived object.
    void *object = dynamic_cast<void>(aObject); // crashes in this
    line????
    //......

    }
    , Jan 17, 2007
    #1
    1. Advertising

  2. Andre Kostur Guest

    "" <> wrote in
    news::

    > Hi,
    >
    > I have a c++ application which crashes in this line (from the debugger,
    > I have a segmentation fault here):
    > void *object = dynamic_cast<void>(aObject);
    >
    >
    > I have stepped thru the code in debugger and the 'aObject' pointer is
    > Not null. So can you please tell me, what else can go wrong so that my
    > program crashes in this line?
    >
    > Thank you. for any help.
    >
    >
    >
    > Code:
    >
    > NS_IMETHODIMP
    > nsTraceRefcntImpl::LogAddCOMPtr(void* aCOMPtr,
    > nsISupports* aObject)
    > {
    > #if defined(NS_BUILD_REFCNT_LOGGING) &&
    > defined(HAVE_CPP_DYNAMIC_CAST_TO_VOID_PTR)
    > // Get the most-derived object.
    > void *object = dynamic_cast<void>(aObject); // crashes in this
    > line????
    > //......
    >
    > }



    dynamic_cast to a void? I thought you had to either dynamic_cast to either
    a pointer (and check the return value for NULL) or a reference (and be
    prepared for a bad_cast exception).
    Andre Kostur, Jan 17, 2007
    #2
    1. Advertising

  3. Guest

    That is code from others. But i would like to investigate when the
    program crashes at that line.

    Thank you.

    Andre Kostur wrote:
    > "" <> wrote in
    > news::
    >
    > > Hi,
    > >
    > > I have a c++ application which crashes in this line (from the debugger,
    > > I have a segmentation fault here):
    > > void *object = dynamic_cast<void>(aObject);
    > >
    > >
    > > I have stepped thru the code in debugger and the 'aObject' pointer is
    > > Not null. So can you please tell me, what else can go wrong so that my
    > > program crashes in this line?
    > >
    > > Thank you. for any help.
    > >
    > >
    > >
    > > Code:
    > >
    > > NS_IMETHODIMP
    > > nsTraceRefcntImpl::LogAddCOMPtr(void* aCOMPtr,
    > > nsISupports* aObject)
    > > {
    > > #if defined(NS_BUILD_REFCNT_LOGGING) &&
    > > defined(HAVE_CPP_DYNAMIC_CAST_TO_VOID_PTR)
    > > // Get the most-derived object.
    > > void *object = dynamic_cast<void>(aObject); // crashes in this
    > > line????
    > > //......
    > >
    > > }

    >
    >
    > dynamic_cast to a void? I thought you had to either dynamic_cast to either
    > a pointer (and check the return value for NULL) or a reference (and be
    > prepared for a bad_cast exception).
    , Jan 17, 2007
    #3
  4. red floyd Guest

    wrote:
    > Hi,
    >
    > I have a c++ application which crashes in this line (from the debugger,
    > I have a segmentation fault here):
    > void *object = dynamic_cast<void>(aObject);
    >
    >
    > I have stepped thru the code in debugger and the 'aObject' pointer is
    > Not null. So can you please tell me, what else can go wrong so that my
    > program crashes in this line?
    >
    > Thank you. for any help.
    >
    >
    >
    > Code:
    >
    > NS_IMETHODIMP
    > nsTraceRefcntImpl::LogAddCOMPtr(void* aCOMPtr,
    > nsISupports* aObject)
    > {
    > #if defined(NS_BUILD_REFCNT_LOGGING) &&
    > defined(HAVE_CPP_DYNAMIC_CAST_TO_VOID_PTR)
    > // Get the most-derived object.
    > void *object = dynamic_cast<void>(aObject); // crashes in this
    > line????
    > //......
    >
    > }
    >


    The cast should be to void*, not to void.

    Per 5.2.7, it is legal to dynamic cast to void* (specifically 5.2.7/1
    and 5.7.7/7).

    To be honest, I'm surprised your fragment compiled if it really is
    <void> and not <void*>.
    red floyd, Jan 17, 2007
    #4
  5. Default User Guest

    Re: C++ application which crashes in dynamic_cast - TPA

    wrote:

    >
    > That is code from others. But i would like to investigate when the
    > program crashes at that line.


    Please don't top-post. Your replies belong following or interspersed
    with properly trimmed quotes. See the majority of other posts in the
    newsgroup, or the group FAQ list:
    <http://www.parashift.com/c++-faq-lite/how-to-post.html>
    Default User, Jan 17, 2007
    #5
    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. alg

    dynamic_cast<>

    alg, Jul 14, 2003, in forum: C++
    Replies:
    3
    Views:
    464
    Rolf Magnus
    Jul 14, 2003
  2. Dan Noland

    dynamic_cast and references

    Dan Noland, Jul 29, 2003, in forum: C++
    Replies:
    0
    Views:
    485
    Dan Noland
    Jul 29, 2003
  3. Yuming Ma
    Replies:
    1
    Views:
    691
    Jeff Schwab
    Dec 17, 2003
  4. Andreas Sch.

    typeid and dynamic_cast, gcc 3.3

    Andreas Sch., Jan 23, 2004, in forum: C++
    Replies:
    18
    Views:
    1,866
    Janusz Szpilewski
    Jan 29, 2004
  5. Jamie Burns
    Replies:
    11
    Views:
    8,931
    Nick Hounsome
    Jan 29, 2004
Loading...

Share This Page