11

I am using dynamic_cast in my source to cast pointer as some thing like below,

Base *base = here storing the pointer;

Derived *derived = dynamic_cast<Derived*>(base);

In the case of base doesn't have the pointer of the class hierarchy then cast fails and returns NULL. In next lines I am checking NULL. So no issues.

I came across a crash dump, my application crashed due to the dynamic_cast throws exception.

I know dynamic_cast will throw only when used with reference types.

Any idea when the dynamic_cast can throw exception when used with pointer as I used in above source?enter image description here

srajeshnkl
  • 883
  • 3
  • 16
  • 49

2 Answers2

21

Any idea when the dynamic_cast can throw exception when used with pointer as I used in above source?

In a well-defined program, it cannot. The standard does not allow it:

[C++11: 5.2.7/9]: The value of a failed cast to pointer type is the null pointer value of the required result type. A failed cast to reference type throws std::bad_cast (18.7.2).

However, if you pass dynamic_cast an invalid pointer, then you invoke undefined behaviour and anything may happen, including some implementation-defined C++ exception, or a runtime crash.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
6

dynamic_cast<Derived*> can throw if the pointer passed to it (base) is invalid, since dynamic_cast needs to dereference it in order to know its dynamic type.

EDIT: To be more specific. dynamic_cast will never throw a structured exception (std::bad_cast, for instance) when used with pointers, but it will probably throw an unstructured exception that you cannot catch when passed an invalid pointer. Using invalid pointers causes undefined behaviour, which in this case usually means access to invalid memory and a crash.

Based on the memory dump you have attached to your question, it is clear that pInfo points to an invalid object, hence all those <Memory access error> messages. This means that pInfo is an invalid pointer and this is the reason why your program crashes. You have a bug somewhere and you will have to fix it.

Gorpik
  • 10,940
  • 4
  • 36
  • 56
  • As per my analyzis, I came to know, in the case of invalid pointer it's giving NULL. When it can thow exception istead of returning NULL. Can you please give your inputs on what you mean invalid pointer here. – srajeshnkl May 10 '13 at 06:59
  • @RajeshSubramanian An invalid pointer is a pointer that does not point to an object nor is null. If `base` is pointing to a destructed object or has not been initialised to an object's address, it is invalid. – Gorpik May 10 '13 at 07:14
  • In my case from the crashdump I found that the object is not null; because before this cast I have a condition for checking null. – srajeshnkl May 10 '13 at 07:48
  • If it were null you would have no problem: a `dynamic_cast` of null always yields null. But is the pointer pointing to a valid object? A valid object is one that has been constructed but not yet destroyed. – Gorpik May 10 '13 at 07:50
  • The object is not null. But few of the member values it contains are not accessible. Will that make the dynamic_cast to become fail? I have attached the watch window of this object from windbg in my actual question now. (pInfo) is the object passed to dynamic_cast. – srajeshnkl May 10 '13 at 08:00
  • An object is never null; only a pointer can point to null. If those member values are "not accessible", it means that it is not valid. And yes, this will cause `dynamic_cast` to fail. – Gorpik May 10 '13 at 10:16
  • Thanks for your response. Yes I wrongly mentioned Object instead of pointer. – srajeshnkl May 10 '13 at 11:52