Olá,
Estou escrevendo um projeto em C++. Preciso utilizar atributos de classe (estáticos). Algumas classes do Projeto utilizam funções estáticas sem a necessidade de atributos estáticos. Isso funciona corretamente. Porém, quando precisei criar um servico de tempo e adicionar atributos de estáticos na classe, o codigo não funciona na placa. Contudo, eu testei na simulação e funcionou sem problemas. Além disso, existem outras classes no mesmo projeto que também utilizam atributos estáticos e até agora não tive problemas. Segue o código .h e .cpp.
Hello,
I'm writing a project in C ++. I need to use class attributes (static). Some classes of the Project use static functions without the need for static attributes and this works correctly. However, when I needed to create a service time and add static attributes in this class, the code did not work on the board. However, I have tested in simulation and worked without problems. In addition, there are other classes in the same project that also use static attributes and so far had no problems. Here is the code .h and .cpp
//---------------- Header File (TimeService.h) namespace service { class TimeService : public Service{ public: TimeService(hal::Timer* timer); static void wait(U32 miliseconds); virtual void init(); private: static hal::Timer* timer; static U32 timeCount; }; } //----------------- End Header File (TimeService.h) //---------------- CPP File (TimeService.cpp) hal::Timer* service::TimeService::timer; U32 service::TimeService::timeCount; namespace service { TimeService::TimeService(hal::Timer* timer) { TimeService::timeCount = 0; TimeService::timer = timer; } void TimeService::init() { //DO NOTHING. } void TimeService::wait(U32 miliseconds) { TimeService::timeCount = ((miliseconds* TimeService::timer->getTickInterval()) / 1000); while(TimeService::timeCount > 0); } } //----------------- End CPP File (TimeService.cpp)
When i remove the contents of all functions, including the constructor, the program works. The program always simulate whitout problems.
I'm not so sure this has anything to do with C++. What do you mean by "does not work"? What does happen?
void TimeService::wait(U32 miliseconds) { TimeService::timeCount = ((miliseconds* TimeService::timer->getTickInterval()) / 1000); while(TimeService::timeCount > 0); }
notice that if "getTickInterval" returns a value larger or equal to 1000, you program hangs in the while loop, unless that member is updated on another context!
So what's happening?
When I say "does not work" I mean that the display shows nothing, different from before I write this class. Indeed, none of the class functions is called, except the constructor (this class is statically instantiated such as all of other classes). When I remove the content of the functions and the constructor, the display shows the text it showed before. The most intriguing is that there is other class with static member variable and there was no problem.
It is impossible to tell what's going on based on your description. A stack overflow, maybe? Do you have a debugger? What happens when the object is instantiated?
Only the constructor call will require stack space.
The object itself is - as described - statically initialized which I guess means it is declared as a global variable or as static inside another class or inside a fucntion. So the object should be in global data space. And the two member fields in the object are static so they should be in global data space.
Maybe there is a memory overwrite somewhere - then adding/removing variables may change the result of the memory overwrite. Sometimes hiding the bad effects.
By the way - your code is incomplete, since there are no function anywhere that have access rights to the timeCount member variable and may tick this down. And the members are private, not protected, and without any friend declaration. So no other code may access the variables.
I'm sorry, I forgot the tick member function:
void TimeService::tick() { if(TimeService::timeCount) { TimeService::timeCount--; } }
However, I cannot understand why this problem happens. Would it be a compiler bug? Is there a limit for static variables declaration? What could be the cause?
Start with a small test program that contains the idea you are trying to use here. Does it still fail? Did you debug your failed program?
The only open issue is when constructors for global objects are called in relation to the initialization of DATA and BSS and the configuration of the stack(s).
But the word "static" for member fields in a class just means that the members will be shared between all objects of that type, and stored as a global variable. And if the object itself is declared static inside a function, it will be stored as an unnamed global object.
What does the base class constructor do?