2

I am trying to dynamic_cast, one virtual class, to another:

SomeClass::SomeFunc(_AListner& listner)
_BListner* listner = dynamic_cast<_BListner*>(&listner) 

Please note that class _BListner is derived from class _AListner.

But I get this error on debugging:

SIGSEGV, Segmentaion fault.

<some address> in  __dynamic_cast () from /usr/lib/libstdc++
Pshemo
  • 122,468
  • 25
  • 185
  • 269
V.K Singh
  • 47
  • 1
  • 6
  • 2
    Can you show the code that invokes `SomeFunc()` ? Of particular interest is the lifetime of `listner`. – hmjd Aug 02 '12 at 08:20
  • Also, you should use a different naming convention: http://stackoverflow.com/questions/228783/what-are-the-rules-about-using-an-underscore-in-a-c-identifier : `Each name that [...] begins with an underscore followed by an uppercase letter is reserved to the implementation for any use.` – BoBTFish Aug 02 '12 at 08:38
  • Could be anything, post the code. – john Aug 02 '12 at 08:48
  • Hi All, Thanks for reply Moreover SomeFunc() is a callback function from somewhere else.so I dont know what exactly appens there. Also the parameters are not nulls. – V.K Singh Aug 02 '12 at 09:12
  • @V.KSingh, a non-null argument does not guarantee that object being pointed to or referred to exists. – hmjd Aug 02 '12 at 09:18
  • @hmjd- yes I know and after dynamic_cast listner is null. any idea how to catch exception thrown by dynamic_catch and why is it null or how to check if the address shown is valid or not? I am doing gdb debugging. – V.K Singh Aug 02 '12 at 09:31

2 Answers2

1

SIGSEGV can be triggered by dynamic_cast when:

  • the object has a wild address
  • the object's metadata (typically vtable pointer) has been corrupted
  • the object doesn't match the static type of the expression

or

  • the expression doesn't have polymorphic type (that means the static type of the expression passed to dynamic_cast, not merely the dynamic type of the object it points into).

The last case should be caught by the compiler though.

Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
  • I had this problem when I `dynamic_cast`'ed a pointer that I had taken from a `std::vector<...>::iterator` which had been invalidated by erasing elements of the vector. I think that counts as having a wild address. – Algoman Sep 25 '18 at 10:22
0

You mentioned _BListner is derived from _AListner. Do you have any third party class like _CListner which is also derivde from _AListner? If so, passing a _CListner Object to your function is allowed, but the dynamic_cast fails and throws an exception of type std::bad_cast. Not catching that exception eventually may result in SIGSEGV.

Just an assumption, you definitely need to provide more information.

sebm
  • 1
  • Hi All, Thanks for reply there is nothing like _CListner. Moreover SomeFunc() is a callback function from somewhere else.so I dont know what exactly appens there. Also the parameters are not nulls. – V.K Singh Aug 02 '12 at 09:10
  • Try to call any virtual method from listener just before doing dynamic_cast. If the listener is corrupted this will probably result a crash, and at least you will know that the problem is not in dynamic_cast. Also using of tools like Valgrind may be helpful. – ovk Aug 02 '12 at 09:46
  • 2
    The dynamic_cast will _not_ throw std::bad_cast in the example. dynamic_cast simply returns NULL when used with a pointer argument that cannot be casted. – Black Aug 02 '12 at 10:43
  • @Black:But that is not being the case. if it returns null, I can handle but it is crashing with SIGSEGV. – V.K Singh Aug 02 '12 at 11:54
  • @Singh: Do you handle the NULL pointer? If not, it may cause the SIGSEGV. The point I want to make is that sebms' answer is wrong. dynamic_cast doesn't throw exceptions with pointer arguments and an uncaught std::bad_cast does not cause SIGSEGV but a call to `terminate()` (see http://www.cplusplus.com/reference/std/exception/terminate/ ) – Black Aug 02 '12 at 14:42