2

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?

Joseph Quinsey
  • 9,553
  • 10
  • 54
  • 77
ryan0270
  • 1,135
  • 11
  • 33
  • How are you setting the variable mName in the parent class? – Izza Jan 23 '12 at 08:24
  • Have you defined `getName()` in the other derived classes also ? – iammilind Jan 23 '12 at 08:31
  • ISystemModel and ISystemModelAffine don't set mName and the other two classes just set it explicitly in their constructor (e.g. mName = "";) getName() is not defined anywhere else (for these classes) – ryan0270 Jan 23 '12 at 09:31

3 Answers3

1

Make sure anything that's a QObject is allocated on the heap, not the stack. They don't play too well with the stack.

Kai
  • 38,985
  • 14
  • 88
  • 103
chad
  • 26
  • 1
0

I guess that you call virtual function from constructor that not allowed and may cause the unexpected behavior. You could check it :).

AlexTheo
  • 4,004
  • 1
  • 21
  • 35
0

One possibility is that your virtual table(s) are getting corrupted somehow (heap/stack overflow or incorrect pointer dereference, if that's even possible) and so the virtual call to getName() fails. When changing it to a non-virtual it succeeds as it doesn't touch the virtual table. Try other virtual calls on the object to see if anything else doesn't work.

If it is corruption then it could have happened anytime prior to this point. Add logging or trace through and see where calls to getName() begin to fail.

uesp
  • 6,194
  • 20
  • 15