C++Visual Studio Release生成未使用的代码崩溃

C++ Visual Studio Release build unused code crash

本文关键字:代码 崩溃 未使用 Studio Release C++Visual      更新时间:2023-10-16

我有一个问题很一般,但我希望有人至少能给我指明正确的方向。

我创建了我的项目,我只是在带有/MDd标志的调试模式下构建它。但它开始出现性能问题,所以我想在发布模式下尝试一下,看看进展如何。问题是,当我使用/MD或/MT标志和Release模式时,我的应用程序会立即崩溃。所以我试着找出原因。它在调试中运行良好。我尝试了一些代码更改,但没有任何帮助。所以我决定让我的应用程序启动并注释掉我的其余代码。但它仍然在崩溃。即使我的代码未被使用。当我完全删除了代码中那些未使用的部分时,它并没有崩溃。

我认为这是一种带有变量inicialization/声明的东西,但我不太确定我应该寻找什么。

有人能告诉我是什么导致应用程序崩溃,即使它只是声明/嵌入,甚至没有在运行时中使用?

我希望你能以某种方式理解我的问题所在。

谢谢你的建议!

EDIT:当项目中有未使用的代码时,它会崩溃,但当我删除未使用的编码时不会崩溃。

#include "core/oxygine.h"
#include "Stage.h"
#include "DebugActor.h"
//#include "Galatex.h"

using namespace oxygine;

//called each frame
int mainloop()
{
//galatex_update();
//update our stage
//update all actors. Actor::update would be called also for all children
getStage()->update();
if (core::beginRendering())
{
Color clearColor(32, 32, 32, 255);
Rect viewport(Point(0, 0), core::getDisplaySize());
//render all actors. Actor::render would be called also for all children
getStage()->render(clearColor, viewport);
core::swapDisplayBuffers();
}
//update internal components
//all input events would be passed to Stage::instance.handleEvent
//if done is true then User requests quit from app.
bool done = core::update();
return done ? 1 : 0;
}
//it is application entry point
void run()
{
ObjectBase::__startTracingLeaks();
//initialize Oxygine's internal stuff
core::init_desc desc;
#if OXYGINE_SDL || OXYGINE_EMSCRIPTEN
//we could setup initial window size on SDL builds
desc.w = 1800;
desc.h = 1000;
//marmalade settings could be changed from emulator's menu
#endif

//galatex_preinit();
core::init(&desc);

//create Stage. Stage is a root node
Stage::instance = new Stage(true);
Point size = core::getDisplaySize();
getStage()->setSize(size);
//DebugActor is a helper actor node. It shows FPS, memory usage and other useful stuff
DebugActor::show();
//initialize this example stuff. see example.cpp
//galatex_init();
#ifdef EMSCRIPTEN
/*
if you build for Emscripten mainloop would be called automatically outside.
see emscripten_set_main_loop below
*/
return;
#endif

//here is main game loop
while (1)
{
int done = mainloop();
if (done)
break;
}
//user wants to leave application...
//lets dump all created objects into log
//all created and not freed resources would be displayed
ObjectBase::dumpCreatedObjects();
//lets cleanup everything right now and call ObjectBase::dumpObjects() again
//we need to free all allocated resources and delete all created actors
//all actors/sprites are smart pointer objects and actually you don't need it remove them by hands
//but now we want delete it by hands
//check example.cpp
//galatex_destroy();

//renderer.cleanup();
/**releases all internal components and Stage*/
core::release();
//dump list should be empty now
//we deleted everything and could be sure that there aren't any memory leaks
ObjectBase::dumpCreatedObjects();
ObjectBase::__stopTracingLeaks();
//end
}
#ifdef __S3E__
int main(int argc, char* argv[])
{
run();
return 0;
}
#endif

#ifdef OXYGINE_SDL
#include "SDL_main.h"
extern "C"
{
int main(int argc, char* argv[])
{
run();
return 0;
}
};
#endif
#ifdef EMSCRIPTEN
#include <emscripten.h>
void one() { mainloop(); }
int main(int argc, char* argv[])
{
run();
emscripten_set_main_loop(one, 0, 0);
return 0;
}
#endif

所以我会在这里为其他像我这样的新手写这篇文章,他们可能会发现自己处于类似的困境。

我的问题是初始化静态变量和其他"函数外"的变量。例如:

MyObject object = new MyObject();    //This was the reason, why it was crashing, just had to move
// initialization of such variables to function which was called with object creation.                
void MyClass::myFunction(){
object->doSomething();      
}

所以当程序开始对这些变量进行初始化时,导致程序崩溃。注意:这似乎是对象的问题,因为Integers之类的变量很好。

好吧,我不完全确定为什么在调试模式下允许这样做,但在启动后立即崩溃发布模式,也许有人可以在这个评论下回答并解释这种行为,我只是一个工程师,我做了很多坏事,但我正在尝试,这很好,对吧?:D

我希望我没有浪费太多的时间,伙计们,也许这篇文章会对未来的人有用。