Skip to content

Commit

Permalink
Add backtrace to type erasure casting to quickly identify where the i…
Browse files Browse the repository at this point in the history
…ssue location
  • Loading branch information
Levi-Armstrong committed Jun 7, 2024
1 parent 6f9582d commit 63f9a4b
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
2 changes: 2 additions & 0 deletions tesseract_common/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ target_link_libraries(
Boost::filesystem
Boost::serialization
console_bridge::console_bridge
boost_stacktrace_backtrace
backtrace # important for boost::stacktrace to generate function names and line numbers
yaml-cpp)
target_compile_options(${PROJECT_NAME} PUBLIC ${TESSERACT_COMPILE_OPTIONS_PUBLIC})
target_compile_definitions(${PROJECT_NAME} PUBLIC ${TESSERACT_COMPILE_DEFINITIONS})
Expand Down
9 changes: 7 additions & 2 deletions tesseract_common/include/tesseract_common/type_erasure.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,11 @@
#ifndef TESSERACT_COMMON_TYPE_ERASURE_H
#define TESSERACT_COMMON_TYPE_ERASURE_H

#define BOOST_STACKTRACE_USE_BACKTRACE

#include <memory>
#include <typeindex>
#include <boost/stacktrace.hpp>
#include <boost/serialization/base_object.hpp>
#include <boost/serialization/nvp.hpp>
#include <boost/serialization/assume_abstract.hpp>
Expand Down Expand Up @@ -218,7 +221,8 @@ struct TypeErasureBase
{
if (getType() != typeid(T))
throw std::runtime_error("TypeErasureBase, tried to cast '" + std::string(getType().name()) + "' to '" +
std::string(typeid(T).name()) + "'!");
std::string(typeid(T).name()) + "'\nBacktrace:\n" +
boost::stacktrace::to_string(boost::stacktrace::stacktrace()) + "\n");

auto* p = static_cast<uncvref_t<T>*>(value_->recover());
return *p;
Expand All @@ -229,7 +233,8 @@ struct TypeErasureBase
{
if (getType() != typeid(T))
throw std::runtime_error("TypeErasureBase, tried to cast '" + std::string(getType().name()) + "' to '" +
std::string(typeid(T).name()) + "'!");
std::string(typeid(T).name()) + "'\nBacktrace:\n" +
boost::stacktrace::to_string(boost::stacktrace::stacktrace()) + "\n");

const auto* p = static_cast<const uncvref_t<T>*>(value_->recover());
return *p;
Expand Down

0 comments on commit 63f9a4b

Please sign in to comment.