What could cause a dynamic_cast to crash?(什么可能导致 dynamic_cast 崩溃?)
问题描述
我有一段代码如下:
有时会崩溃:
我不知道它为什么会崩溃.obj 不为空(如果是这样就没有问题,是吗?).
I have no clue why it crashes. obj is not null (and if it was it would not be a problem, would it ?).
动态转换崩溃的原因可能是什么?
What could be the reason for a dynamic cast to crash ?
如果它不能强制转换,它应该只返回 NULL 否?
If it can't cast, it should just return NULL no ?
推荐答案
崩溃的一些可能原因:
obj
指向具有非多态类型的对象(没有虚拟方法的类或结构,或基本类型).obj
指向一个已经被释放的对象.obj
指向未映射的内存,或已映射的内存以在访问时产生异常的方式(例如保护页或不可访问的页).obj
指向具有多态类型的对象,但该类型是在禁用 RTTI 的情况下编译的外部库中定义的.
obj
points to an object with a non-polymorphic type (a class or struct with no virtual methods, or a fundamental type).obj
points to an object that has been freed.obj
points to unmapped memory, or memory that has been mapped in such a way as to generate an exception when accessed (such as a guard page or inaccessible page).obj
points to an object with a polymorphic type, but that type was defined in an external library that was compiled with RTTI disabled.
并非所有这些问题都会在所有情况下导致崩溃.
Not all of these problems necessarily cause a crash in all situations.
这篇关于什么可能导致 dynamic_cast 崩溃?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!