This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Using Interface leads to hard fault

Hello Forums,

i´m starting to interfaces in C++ on a stm32f3 under µVision4.
To start of i created an interface as simple as possible

#ifndef _ICNLSUPPL_H_
#define _ICNLSUPPL_H_

class IChnlSupplyerr
{
        public:
                IChnlSupplyerr(){};
                ~IChnlSupplyerr(){};
        virtual signed short doIt(signed short arg) = 0;

};

#endif


I have made two classes, which implementing the "doIt" method. One simply adds 3 and the other subtracts 2.
In a third class i instantiate these classes and store pointer of each in an array of the Interfaces type.

#include "Chnl.h"

Chnl::Chnl()
{
        objArr[0] = &add3;
        objArr[1] = &sub2;

}

signed short Chnl::Run(unsigned short sel, signed short arg)
{
        if(sel == 0) arg = objArr[0] -> doIt(arg);
        if(sel == 1) arg = objArr[1] -> doIt(arg);
        return arg;
}

The wired thing is, if i instantiate an object of the "Chnl" class directly in the main file, everything works as expected. But if i do the same thing in an object below the main, i can track witch the debugger that the hard fault handler is called when it commes to if(sel == 0) arg = objArr[0] -> doIt(arg);
After some trying "this and that" i think, that the adresses change somehow from the constructor until the acctual call. I wrote an "If" comparing like If(objArr[0] == &add3) "switch on led".

Has someone an idea, what i´mdoing wrong?

Parents Reply Children
No data