When dynamic_cast will throw exception in case used with pointer?

前端 未结 2 930
独厮守ぢ
独厮守ぢ 2021-02-20 07:20

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

        
2条回答
  •  北恋
    北恋 (楼主)
    2021-02-20 08:02

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

提交回复
热议问题