---
title: Dynamic type violation
framework: xcode
role: article
role_heading: Article
path: xcode/dynamic-type-violation
---

# Dynamic type violation

Detects when an object has the wrong dynamic type.

## Overview

Overview Use this check to detect when an object has the wrong dynamic type. Dynamic type violations can cause unintended code execution. Available in Xcode 9 and later. Member call on instance with incorrect type in C++ In the following code, reinterpret_cast creates a variable with the wrong dynamic type: struct Animal {     virtual const char *speak() = 0; }; struct Cat : public Animal {     const char *speak() override {         return "meow";     } }; struct Dog : public Animal {     const char *speak() override {       return "woof";     } }; auto *dog = reinterpret_cast<Dog *>(new Cat); // Error: dog has incorrect dynamic type dog->speak(); // Error: this call has undefined behavior The method call dog->speak() is suspect. If the speak method in Dog is final override, dog->speak() may return "woof" because the optimizer can devirtualize the call; if not, it might return "meow". note: This UBSan check requires runtime type information, and is incompatible with the -fno-rtti compiler flag. Solution Use reinterpret_cast sparingly, and only when it’s possible to verify that the cast object is an instance of the destination type.

## See Also

### Undefined Behavior Sanitizer

- [Misaligned pointer](xcode/misaligned-pointer.md)
- [Invalid Boolean value](xcode/invalid-boolean.md)
- [Out-of-bounds array access](xcode/out-of-bounds-array-access.md)
- [Invalid enumeration value](xcode/invalid-enumeration-value.md)
- [Reaching of unreachable point](xcode/reaching-of-unreachable-point.md)
- [Invalid float cast](xcode/invalid-float-cast.md)
- [Division by zero](xcode/division-by-zero.md)
- [Nonnull argument violation](xcode/nonnull-argument-violation.md)
- [Nonnull return value violation](xcode/nonnull-return-value-violation.md)
- [Nonnull variable assignment violation](xcode/nonnull-variable-assignment-violation.md)
- [Null reference creation and null pointer dereference](xcode/null-reference-creation-and-null-pointer-dereference.md)
- [Invalid object size](xcode/invalid-object-size.md)
- [Invalid shift](xcode/invalid-shift.md)
- [Integer overflow](xcode/integer-overflow.md)
- [Invalid variable-length array](xcode/invalid-variable-length-array.md)
