C++ application which crashes in dynamic_cast

Y

yinglcs

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

}
 
A

Andre Kostur

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

yinglcs

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

Thank you.
 
R

red floyd

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

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top