C++ dynamic_cast access violation

0

I'v read some of the similar titled questions and seem not much relevant.

I'm using Clang LibTooling, the following code in ASTVisitor:

virtual bool VisitCXXRecordDecl(CXXRecordDecl *d) {
    bool b1 = std::is_polymorphic<NamedDecl>::value;
    bool b2 = std::is_polymorphic<CXXRecordDecl>::value;
    auto dd = dynamic_cast <NamedDecl*>(d);
    auto ddd = dynamic_cast <CXXRecordDecl*>(dd);
    return true;
}

The second dynamic cast will trigger

"0xC0000005: Access violation reading location 0x0000000000000004."

in VS2017.

The classes's relations are here http://clang.llvm.org/doxygen/classclang_1_1CXXRecordDecl.html.

Change to static_cast solve the problem.

b1 and b2 are both true. dd is successfully casted, here is the "Locals" pane in VS2019 before executing the second dynamic cast:

+       d   ... clang::CXXRecordDecl *
+       dd  ... clang::NamedDecl * {clang::CXXRecordDecl}

In the route from NamedDecl to CXXRecordDecl, there is a multiple inheritence :

class TagDecl : public TypeDecl, public DeclContext, public Redeclarable<TagDecl>

I don't know whether this is relevant.

c++
dynamic-cast
asked on Stack Overflow Dec 26, 2019 by jw_ • edited Dec 26, 2019 by jw_

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0