C ++ Visual Studio Release构建未使用的代码崩溃

时间:2016-05-24 19:34:20

标签: c++ visual-studio

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

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

我认为这是变量inicialization / declaration,但我不太确定我应该寻找什么。

有人可以建议我什么可能导致应用程序崩溃,即使它只是声明/ Inicialization并且甚至没有在RunTime中使用?

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

感谢您的任何建议!

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

    #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

1 个答案:

答案 0 :(得分:0)

所以我会在这里写一下像我这样的其他新手,他们会发现自己处于类似的期待中。

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

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

所以当程序启动那些变量的inicialization时会导致程序崩溃。 注意:它似乎是对象的问题,导致像Integers这样的变量就好了。

嗯,我不完全确定为什么在调试模式下允许这样做,但是在启动后立即崩溃发布模式,也许有人可以在这个评论下回答并解释这个行为,我只是打招呼而且我做得很多不好的东西,但我在努力,这很好,对吧? :d

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