静态析构函数被提前调用

发布于 2024-12-23 13:59:33 字数 1591 浏览 0 评论 0原文

尽管我读过类似的问题,但这个问题似乎与典型问题完全相反(静态析构函数未被调用)。我正在用 C++ 编写一个游戏引擎,其中有几个变量作为静态类成员。但是,似乎我没有正确初始化或使用它,因为每当我尝试调用静态成员的析构函数时都会调用它。这是成员的定义和声明:(

static CRendering RENDER_PIPELINE;

在 CDisplay.h 中)

CRendering CDisplayCore::RENDER_PIPELINE;

(在 CDisplay.cpp 中)

这是一个调用堆栈,显示析构函数调用,就在我使用静态变量的一种方法之后:

#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)

CEntity::UpdateGameLogic 中的代码是:

CDisplay::_Render().RenderTexture(...);

RenderTexture是CRendering的一种方法。 _Render() 是成员的静态 getter。

这里可能有什么问题?

编辑 _Render() 的定义

static inline CRendering _Render()
{
    return RENDER_PIPELINE;
}

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;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

莳間冲淡了誓言ζ 2024-12-30 13:59:33

您的 _Render() 函数返回 CRendering 对象的副本。尝试将其更改为:

static inline CRendering &_Render()

上面的声明将返回单个静态CRendering对象的引用。如果没有 &,C++ 将创建整个对象的副本,并从您的函数返回该副本(然后使用返回值的代码将在调用完成后立即调用该副本的析构函数) )。

Your _Render() function returns a copy of the CRendering object. Try changing it to:

static inline CRendering &_Render()

The above declaration will return a reference to the single static CRendering object. 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).

裂开嘴轻声笑有多痛 2024-12-30 13:59:33
static inline CRendering _Render()
{
    return RENDER_PIPELINE;
}

您将按值返回 CRendering,即副本。你看到的可能是副本的析构函数。

也许你想要

static inline CRendering &_Render()
static inline CRendering _Render()
{
    return RENDER_PIPELINE;
}

You're returning CRendering by value, that is, a copy. What you see is probably a destructor of the copy.

Maybe you wanted

static inline CRendering &_Render()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文