I’m wondering what the possible causes of an MCU halting on a polymorphic function call might be.
I have the following code. I can call the member function directly on the non-pointer variable. However, once I cast the variable to a pointer of it’s parent virtual class and try calling the member function through polymorphism, the mcu halts.
Any help is greatly appreciated.
// parent.h
#ifndef WAVES_2_DSP_ENGINE_PARENT_H
#define WAVES_2_DSP_ENGINE_PARENT_H
namespace waves2 {
class Parent {
public:
Parent() {}
~Parent() {}
virtual void Init() = 0;
};
}
#endif
// child.h
#ifndef WAVES_2_DSP_ENGINE_CHILD_H
#define WAVES_2_DSP_ENGINE_CHILD_H
#include "waves2/dsp/engine/parent.h"
namespace waves2 {
class Child: public Parent
{
public:
Child() { }
~Child() { }
virtual void Init();
private:
DISALLOW_COPY_AND_ASSIGN(Child);
};
}
#endif
// child.cc
#include "waves2/dsp/engine/child.h"
namespace waves2 {
void Child::Init() {
}
}
#endif
// main.cpp
Child child;
int main(void) {
// this works fine
child.Init();
Parent* p = &child;
// If I execute the following code,
// the code which toggles the LED will not be reached
p->Init();
while(1) {
// toggle led
if((system_clock.milliseconds() / 1000) % 2 < 1) {
GPIO_SetBits(GPIOA, GPIO_Pin_5);
} else {
GPIO_ResetBits(GPIOA, GPIO_Pin_5);
}
}
}
10