I'm trying to use g++ (compiler related with this thread) and virtual function, but code crashes on ESP while invoking virtual function.
Sample code:
class A {
public:
void run(){
foo();
}
virtual void foo() = 0;
};
class B : public A{
public:
void foo(){
Serial.println("TEST"); //what ever debug information you need
}
};
B test;
test.foo(); //works, I see TEST in termial
test.run(); //crashes with Exception(28)
Error message:
Fatal exception (28):
epc1=0x4024dfc5
epc2=0x00000000
epc3=0x4024306f
epcvaddr=0x00000000
depc=0x00000000
Does anyone have idea what can be wrong with compiler? The same code with g++ x86 compiler works properly.
Does anyone know, how to use virtual functions (vtable) with esp8266? What kind of parameters should be added to compiler?
Are you running the global constructors manually? This is required because the startup code doesn't do it. See page 1 of this same thread. You can see if this is the case by moving your test code inside a function (void test()) and see if it works, because then it's not a global object anymore.
If you don't, your class B (including virtual function table) will not be initialized.