I have the following class heirarchy:
ICSL::ISystemModel
ICSL::ISystemModelAffine : public ISystemModel
ICSL::Quadrotor::SystemModelQuadrotor : public QObject, ISystemModelAffine
ICSL::Quadrotor::SystemModelQuadrotorSimulated : public public SystemModelQuadrotor
Then, in the another class I define
SystemModelQuadrotor mDynamicModelReal;
SystemModelQuadrotorSimulated mDynamicModelSimulated;
During the initialization I call a bunch of functions to set variables on both mDynamicModelReal
and mDynamicModelSimulated
with no problems. Later on I call some functions from ISystemModel
to access some variables which works fine for mDynamicModelReal
but segfaults for mDynamicModelSimulated
. Checking in gdb, the variable value is correct, and it seems to know what the function is, but it complains when trying to call the actual function (based on my limited understanding of these things). Here is gdb probing after it segfaulted for a call to mDynamicModelSimulated.getName()
(gdb) print mDynamicModelSimulated.mName
$19 = {static npos = <optimized out>, _M_dataplus = {<std::allocator<char>> = {<__gnu_cxx::new_allocator<char>> = {<No data fields>}, <No data fields>}, _M_p = 0x810658 "quadGaui5"}}
(gdb) print mDynamicModelSimulated.getName
$20 = {const std::string (ICSL::ISystemModel * const)} 0x44f506 <ICSL::ISystemModel::getName()>
(gdb) print mDynamicModelSimulated.getName()
Cannot access memory at address 0x4082c00000000030
And here is the definition of getName()
Class ISystemModel {
public:
...
virtual std::string const getName(){return mName;};
...
protected:
...
std::string mName;
...
};
I don't understand why it can't make that function call. I did discover that if I remove "virtual
" from the function definition it doesn't segfault but I don't see why that makes a difference. Where should I be looking for the source of the problem?