[F767ZI, C++] A hard fault occurs during the initialization of an object
I am writing code for the NUCLEO-F767ZI board in C++.
Initially, I have three classes and their objects are defined as global variables in `usermain.cpp` in ${Project}\User\Src\:
SystemClass systemObj(1);
PwmClass servoObj(&htim3);
TLinkClass tlinkObj(&huart3);SystemClass has the pointers of the PwmClass and TLinkClass objects as its members.
// systemlib.hpp
class SystemClass {
public:
uint8_t ID;
PwmClass* pPwm;
TLinkClass* pTLink;
...
SystemClass(uint8_t ID);
};
// baselib.cpp
SystemClass::SystemClass(uint8_t id) {
ID = id;
}The code works nicely as intended. Now, I want SystemClass to contain the PwmClass and TLinkClass objects as its member for simplicity. So the latter two objects are initialized by SystemClass.
// systemlib.hpp
class SystemClass {
public:
uint8_t ID;
PwmClass Pwm[SYS_PWM_NUM_TIMER];
TLinkClass TLink;
SystemClass(uint8_t ID);
};
// usermain.cpp
SystemClass systemObj(1);It's a very simple change, but the code started to throw a hard fault just after the initialization of SystemClass.
I've been struggling to figure out what causes the hard fault in vain. I am almost new to STM32 programming.
I tried to derive a simpler code that reproduces the same result but I failed on that, too. So I attached two STM32CubeIDE projects where one works and the other throws a hard fault. I think you can see the difference using the diff function provided by the IDE.
I want to know what caused the problem and how to avoid it. The solution will be a very important breakthrough for my future projects. Thank you.
