Reboot when declaring object inside main method

Hello every body.

I have created my own objects and when I was testing some configuration I realized about the following:

1. When I declare the objects outside the main method (as global) the program runs perfectly.

2. When I declare the objects inside the main method the microcontroller reboots. 

What could be the alternatives for this or how can I check what the reason of the reboot is?

  • Hi,

    The first thing that comes to mind is a stack overflow issue. A global object is never on the stack, but when you declare it within a function it will be on the stack unless you make it static. So a simple way to see if this could be it is to make it static (by using the static keyword when you declare it) and see if the issue persists. If you are using the nRF Connect SDK you can also add a stack guard and check the debug log to see if that is it (CONFIG_MPU_STACK_GUARD). You can use CONFIG_MAIN_STACK_SIZE to increase the main stack size.

  • The problem is exacly the stack. However, when I put the static keyword to the object inside the main method it does not compile the .cpp file even after I put CONFIG_CPP_STATIC_INIT_GNU=y at the config file.

  • How does it fail now? And did you try to simply increase the stack size? If you don't make progress, perhaps you can upload the failing project here along with instructions on how to build it so that I can have a look?

  • Hi Einar. Thank you for your help.

    I incremented the memory stack size and it worked perfectly (it worked without the static keyword).

    However, I would like to understand some errors that I got during the implementation of this:

    If I put static keyword, I got this error:

    main.cpp:113: undefined reference to `__cxa_guard_release'

    I also tried adding in the prj.conf the following:

    CONFIG_CPP_STATIC_INIT_GNU=y

    But I got the following error:

    error: CPP_STATIC_INIT_GNU (defined at c:\NRF52833\timer\timer_test_1\build\subsys\cpp\Kconfig:78) is assigned in a configuration file,
    but is not directly user-configurable (has no prompt). It gets its value indirectly from other
    symbols. See http://docs.zephyrproject.org/latest/reference/kconfig/CONFIG_CPP_STATIC_INIT_GNU.html
    and/or look up CPP_STATIC_INIT_GNU in the menuconfig/guiconfig interface. The Application
    Development Primer, Setting Configuration Values, and Kconfig - Tips and Best Practices sections of
    the manual might be helpful too.

  • Hi,

    LeoSanta said:
    I incremented the memory stack size and it worked perfectly (it worked without the static keyword).

    That is good to hear. 

    LeoSanta said:
    However, I would like to understand some errors that I got during the implementation of th

    CONFIG_CPP_STATIC_INIT_GNU is not user configurable as the error you got stated, but it is enabled by default - so in any case there is no need to set it. To use it though you need to use non-standard GNU specific way of doing it, but I have not found out how you would do that (and to be frank, my C++ is quite rusty). I do not see enough of your code to say if that would be sensibel anyway though, but I originally intended it just as a simple test (which it turned out not to be Slight smile)

Related