【问题标题】:C++ Visual Studio Release build unused code crashC++ Visual Studio Release 构建未使用的代码崩溃
【发布时间】:2016-05-28 02:03:51
【问题描述】:

我有一个很笼统的问题,但我希望有人能够至少指出我正确的方向。

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

我认为这是具有变量初始化/声明的东西,但我不太确定我应该寻找什么。

有人可以建议我什么会导致应用程序崩溃,即使它只是声明/初始化,甚至没有在运行时使用?

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

感谢您的任何建议!

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

    #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

【问题讨论】:

  • 您需要发布代码(您声称您有最小的崩溃示例)。
  • 如果您在 VS 中从模板创建项目,那么您无需担心所有这些开关。您选择调试或发布。我建议你创建一个新项目并将所有代码复制到其中。
  • 我添加了与未使用的代码一起崩溃的代码,但是当我从项目中完全删除未使用的代码时很好。但我认为这对任何事情都没有帮助。
  • 如果您评论了所有代码 - 正如您所说的 So I decided to make my app just start and comment out rest of my code,那么您有一些项目配置问题。
  • 我已经将代码从项目复制到新项目,但没有帮助。

标签: c++ visual-studio


【解决方案1】:

所以我会在这里为可能遇到类似情况的其他新手写这篇文章。

我的问题在于“函数之外”的静态变量和其他变量的初始化。例如:

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

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

好吧,我不完全确定为什么在调试模式下允许这样做,但在启动后立即崩溃发布模式,也许有人可以在此评论下回答并解释这种行为,我只是初学者,我正在做很多坏东西,但我正在努力,这很好,对吧? :D

我希望我没有浪费你们太多时间,也许这篇文章将来对某人有用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-04-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-14
    • 1970-01-01
    • 2020-06-01
    相关资源
    最近更新 更多