[Answered]: Calling functions in C++ class constructors that have static instances
Posted: Sun Nov 05, 2017 5:30 am
I'm chasing down some strange problems and a thought struck me that I'd like to run past the community. It relates to C++ classes and static / global class instances.
Let us imagine the following class:
If we study this code, we will find that its constructor creates an instance of a semaphore that is presumably used elsewhere in the class. Now also notice that there is a global instance of the class created and made available in the variable called myGlobalInstance.
Let us think about that for a second. Since it is a C++ class instance, that means that its constructor was called. Since its constructor was called, that means that the code defined in the constructor was called. And now comes the key question ... what ESP-IDF/FreeRTOS calls are legitimate within this context?
If the instance weren't defined as global but was instead created in the context of a function ... for example:
I think any and all code/logic would be fair game. However, in a global variable, the code is executed before control is given to main (or app_main() in ESP32). But is this too early? What (if anything) can we claim about the context/state/environment in which constructors defined in global class instances execute within? For example, do they execute before "enough" of ESP-IDF/FreeRTOS is initialized?
Let us imagine the following class:
Code: Select all
class MyClass {
public:
MyClass() {
m_semaphore = xSemaphoreCreate();
...
}
SemaphoreHandle_t m_semaphore;
}
MyClass myGlobalInstance;
main() {
....
}
Let us think about that for a second. Since it is a C++ class instance, that means that its constructor was called. Since its constructor was called, that means that the code defined in the constructor was called. And now comes the key question ... what ESP-IDF/FreeRTOS calls are legitimate within this context?
If the instance weren't defined as global but was instead created in the context of a function ... for example:
Code: Select all
main() {
MyClass myLocalInstance;
}