Issues with Dynamic Casting and Segmentation Fault
Posted: Wed Sep 27, 2023 1:15 pm
Hello,
I am currently encountering an issue related to dynamic casting and segmentation faults in my ARGoS simulation. I have already browsed through previous forum posts and attempted to implement the solutions provided, but the issue persists.
Problem Description:
I am working on a foraging simulation where I use multiple controllers. Initially, I was getting a std::bad_cast error, which, according to previous forum posts, seemed to be due to using dynamic casting with references. I followed the advice to switch to using pointers for dynamic casting as suggested.
Code:
Here is an extended snippet of my code where I loop through the controllers, perform dynamic casting, and print out type IDs:
After running the above snippet, I get the following type of output (formatted here for readability):
The output suggests that some casts to CPFA_controller are successful, while others are failing. Similarly, some casts to Detractor_controller are failing as well. This inconsistency is puzzling, especially since the pointer does not appear to be null when inspected with GDB.
GDB Output:
Shortly after this is where the segmentation fault occurs.
GDB Output:
Steps Taken:
Thank you in advance for your help.
Best regards,
Ryan
I am currently encountering an issue related to dynamic casting and segmentation faults in my ARGoS simulation. I have already browsed through previous forum posts and attempted to implement the solutions provided, but the issue persists.
Problem Description:
I am working on a foraging simulation where I use multiple controllers. Initially, I was getting a std::bad_cast error, which, according to previous forum posts, seemed to be due to using dynamic casting with references. I followed the advice to switch to using pointers for dynamic casting as suggested.
Code:
Here is an extended snippet of my code where I loop through the controllers, perform dynamic casting, and print out type IDs:
Code: Select all
for(argos::CSpace::TMapPerType::iterator it = footbots.begin(); it != footbots.end(); it++) {
argos::CFootBotEntity& footBot = *argos::any_cast<argos::CFootBotEntity*>(it->second);
BaseController* c = dynamic_cast<BaseController*>(&footBot.GetControllableEntity().GetController());
if(c != nullptr) {
cout << "Type of BaseController pointer: " << typeid(c).name() << endl;
CPFA_controller* c2 = dynamic_cast<CPFA_controller*>(c);
Detractor_controller* c3 = dynamic_cast<Detractor_controller*>(c);
if(c2 != nullptr) {
c2->SetLoopFunctions(this);
cout << "CPFA_controller cast successful" << endl;
} else {
cout << "CPFA_controller cast failed" << endl;
cout << "Actual type of c2: " << typeid(c2).name() << endl;
}
if(c3 != nullptr) {
c3->SetLoopFunctions(this);
cout << "Detractor_controller cast successful" << endl;
} else {
cout << "Detractor_controller cast failed" << endl;
cout << "Actual type of c3: " << typeid(c3).name() << endl;
}
} else {
cout << "BaseController cast failed" << endl;
}
}
Code: Select all
Type of BaseController pointer: P14BaseController
CPFA_controller cast successful
Detractor_controller cast failed
Actual type of c3: P20Detractor_controller
Type of BaseController pointer: P14BaseController
CPFA_controller cast successful
Detractor_controller cast failed
Actual type of c3: P20Detractor_controller
...
Type of BaseController pointer: P14BaseController
CPFA_controller cast failed
Actual type of c2: P15CPFA_controller
Detractor_controller cast successful
Type of BaseController pointer: P14BaseController
CPFA_controller cast failed
Actual type of c2: P15CPFA_controller
Detractor_controller cast successful
GDB Output:
Code: Select all
Thread 1 "argos3" hit Breakpoint 1, ...
(gdb) print c
$9 = (CPFA_controller *) 0xbb754be082d0ba00
(gdb) print typeid(c)
$10 = {_vptr.type_info = 0x7ffff7e9bc38 <vtable for __cxxabiv1::__pointer_type_info+16>,
__name = 0x7ffff276a110 <typeinfo name for CPFA_controller*> "P15CPFA_controller"}
GDB Output:
Code: Select all
hread 1 "argos3" received signal SIGSEGV, Segmentation fault.
0x00007ffff2740022 in CPFA_controller::IsHoldingFood (this=0x0) at /home/Ryan/Foraging_Swarm_Defense/CPFA/source/CPFA/CPFA_controller.cpp:161
161 return isHoldingFood;
(gdb) bt
#0 0x00007ffff2740022 in CPFA_controller::IsHoldingFood() (this=0x0) at /home/Ryan/Foraging_Swarm_Defense/CPFA/source/CPFA/CPFA_controller.cpp:161
#1 0x00007ffff2600406 in CPFA_qt_user_functions::DrawOnRobot(argos::CFootBotEntity&) (this=0x555555b9cd80, entity=...)
at /home/Ryan/Foraging_Swarm_Defense/CPFA/source/CPFA/CPFA_qt_user_functions.cpp:19
#2 0x00007ffff260258b in argos::CQTOpenGLUserFunctions::Thunk<CPFA_qt_user_functions, argos::CFootBotEntity>(argos::CEntity&) (this=0x555555b9cd80, c_entity=...)
at /usr/include/argos3/plugins/simulator/visualizations/qt-opengl/qtopengl_user_functions.h:477
#3 0x00007ffff6840616 in argos::CQTOpenGLWidget::paintGL() () at /usr/lib/argos3/libargos3plugin_simulator_qtopengl.so
#4 0x00007ffff5ea5e05 in () at /lib/x86_64-linux-gnu/libQt5Widgets.so.5
#5 0x00007ffff5e832b6 in QWidget::event(QEvent*) () at /lib/x86_64-linux-gnu/libQt5Widgets.so.5
#6 0x00007ffff5e40a66 in QApplicationPrivate::notify_helper(QObject*, QEvent*) () at /lib/x86_64-linux-gnu/libQt5Widgets.so.5
#7 0x00007ffff5e4a0f0 in QApplication::notify(QObject*, QEvent*) () at /lib/x86_64-linux-gnu/libQt5Widgets.so.5
#8 0x00007ffff6818157 in argos::CQTOpenGLApplication::notify(QObject*, QEvent*) () at /usr/lib/argos3/libargos3plugin_simulator_qtopengl.so
#9 0x00007ffff542880a in QCoreApplication::notifyInternal2(QObject*, QEvent*) () at /lib/x86_64-linux-gnu/libQt5Core.so.5
#10 0x00007ffff5e7bf4a in QWidgetPrivate::sendPaintEvent(QRegion const&) () at /lib/x86_64-linux-gnu/libQt5Widgets.so.5
#11 0x00007ffff5ea5c2f in QOpenGLWidget::resizeEvent(QResizeEvent*) () at /lib/x86_64-linux-gnu/libQt5Widgets.so.5
#12 0x00007ffff6842687 in argos::CQTOpenGLWidget::resizeEvent(QResizeEvent*) () at /usr/lib/argos3/libargos3plugin_simulator_qtopengl.so
#13 0x00007ffff5e83947 in QWidget::event(QEvent*) () at /lib/x86_64-linux-gnu/libQt5Widgets.so.5
#14 0x00007ffff5e40a66 in QApplicationPrivate::notify_helper(QObject*, QEvent*) () at /lib/x86_64-linux-gnu/libQt5Widgets.so.5
#15 0x00007ffff5e4a0f0 in QApplication::notify(QObject*, QEvent*) () at /lib/x86_64-linux-gnu/libQt5Widgets.so.5
#16 0x00007ffff6818157 in argos::CQTOpenGLApplication::notify(QObject*, QEvent*) () at /usr/lib/argos3/libargos3plugin_simulator_qtopengl.so
#17 0x00007ffff542880a in QCoreApplication::notifyInternal2(QObject*, QEvent*) () at /lib/x86_64-linux-gnu/libQt5Core.so.5
#18 0x00007ffff5e7be6e in QWidgetPrivate::sendPendingMoveAndResizeEvents(bool, bool) () at /lib/x86_64-linux-gnu/libQt5Widgets.so.5
#19 0x00007ffff5e7fbd7 in QWidgetPrivate::show_helper() () at /lib/x86_64-linux-gnu/libQt5Widgets.so.5
#20 0x00007ffff5e82d6b in QWidgetPrivate::setVisible(bool) () at /lib/x86_64-linux-gnu/libQt5Widgets.so.5
#21 0x00007ffff5e7fb61 in QWidgetPrivate::showChildren(bool) () at /lib/x86_64-linux-gnu/libQt5Widgets.so.5
#22 0x00007ffff5e7fbf3 in QWidgetPrivate::show_helper() () at /lib/x86_64-linux-gnu/libQt5Widgets.so.5
#23 0x00007ffff5e82d6b in QWidgetPrivate::setVisible(bool) () at /lib/x86_64-linux-gnu/libQt5Widgets.so.5
#24 0x00007ffff5e7fb61 in QWidgetPrivate::showChildren(bool) () at /lib/x86_64-linux-gnu/libQt5Widgets.so.5
#25 0x00007ffff5e7fbf3 in QWidgetPrivate::show_helper() () at /lib/x86_64-linux-gnu/libQt5Widgets.so.5
#26 0x00007ffff5e82d6b in QWidgetPrivate::setVisible(bool) () at /lib/x86_64-linux-gnu/libQt5Widgets.so.5
#27 0x00007ffff6836612 in argos::CQTOpenGLRender::Execute() () at /usr/lib/argos3/libargos3plugin_simulator_qtopengl.so
- Switched from dynamic casting with references to pointers.
- Checked for null pointers before accessing methods.
- Validated object lifetimes.
Thank you in advance for your help.
Best regards,
Ryan