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

Hard fault when branching to main

Hi,

I am working in a C++ project for STM3210C-Eval Board (Cortex-M3). I wrote some classes and got the project working perfectly. Then I designed other classes to increment project functionalities and the system no longer works. Debugging, I have noticed that the main function is not executed. So, I started the execution from Reset routine in startup code.

; Reset handler
Reset_Handler    PROC
                 EXPORT  Reset_Handler             [WEAK]
        IMPORT  __main
                 LDR     R0, =__main
                 BX      R0
                 ENDP

When I step into "BX R0" instruction the exception Hard Fault happens and the system hangs in an endless loop. I could see that the value loaded into R0 is 0x08000151, whereas the original value of __main is 0x08000150.

Could someone help me?

  • Hi again,

    I solved the problem. I made a mistake. One of the classes had a recursive constructor. So, whenever the code responsible for static allocation (__main function) was executed it resulted in a hard fault (maybe stack overflow).

    I hope this experience helps other developers.

  • It's a common problem with C++, that people forgets that constructors for global objects are called before main(). So not just recursive constructors but pointer errors, division by zero, exceptions from failed allocations etc can have the program fail before reaching main().