Although I have read the similar questions, this problem seems to be the exact opposite of the typical ones (static destructors not being called). I’m writing a game engine in C++, in which I have several vars as static class members. However, it seems that I am not initiallizing or using it properly, because the destructor for the static member gets called whenever I try to call it. This is the definition and declaration of the member:
static CRendering RENDER_PIPELINE;
(in CDisplay.h)
CRendering CDisplayCore::RENDER_PIPELINE;
(in CDisplay.cpp)
Here is a call stack showing the destructor call, right after I use one method of the static var:
#0 ( Seventh::CRendering::~CRendering(this=0x7fffffffe5f0, __in_chrg=<value optimized out>) (/home/alberto/SeventhEngine/src/Rendering/CRendering.cpp:38)
#1 0x4152d9 Seventh::CEntity::UpdateGameLogic(this=0x8812f0) (/home/alberto/SeventhEngine/src/EntityCore/CEntity.cpp:109)
#2 0x416b68 Seventh::UpdateGameLogicGeneric<std::basic_string<char>, Seventh::CEntity*>(map=...) (include/functors.h:64)
#3 0x416968 Seventh::CEntityManager::UpdateGameLogic(this=0x63dc10) (/home/alberto/SeventhEngine/src/EntityCore/CEntityManager.cpp:65)
#4 0x413122 Seventh::CEngine::UpdateGameLogic(this=0x63dab0) (/home/alberto/SeventhEngine/src/Engine/CEngine.cpp:175)
#5 0x412fe6 Seventh::CEngine::RunGame(this=0x63dab0) (/home/alberto/SeventhEngine/src/Engine/CEngine.cpp:130)
#6 0x40e027 main(argc=1, argv=0x7fffffffe8d8) (/home/alberto/SeventhEngine/main.cpp:31)
The code in CEntity::UpdateGameLogic is:
CDisplay::_Render().RenderTexture(...);
RenderTexture is a method of CRendering. _Render() is an static getter for the member.
What can be the problem here?
Edit Definition of _Render()
static inline CRendering _Render()
{
return RENDER_PIPELINE;
}
Your
_Render()function returns a copy of theCRenderingobject. Try changing it to:The above declaration will return a reference to the single static
CRenderingobject. Without the&, C++ will make a copy of the entire object, and return that from your function (and then your code that uses the return value will call that copy’s destructor immediately after it’s done with the call).