By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,824 Members | 1,247 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,824 IT Pros & Developers. It's quick & easy.

C++ application which crashes in dynamic_cast

P: n/a
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 '07 #1
Share this Question
Share on Google+
4 Replies


P: n/a
"yi*****@gmail.com" <yi*****@gmail.comwrote in
news:11**********************@l53g2000cwa.googlegr oups.com:
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 '07 #2

P: n/a

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

Thank you.

Andre Kostur wrote:
"yi*****@gmail.com" <yi*****@gmail.comwrote in
news:11**********************@l53g2000cwa.googlegr oups.com:
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 '07 #3

P: n/a
yi*****@gmail.com 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
<voidand not <void*>.

Jan 17 '07 #4

P: n/a
yi*****@gmail.com 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>
Jan 17 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.