释放模式错误,但不在调试模式下 [英] release mode error, but not in debug mode

查看:95
本文介绍了释放模式错误,但不在调试模式下的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码在调试模式下运行良好,但在发布模式下失败.

My code runs fine in debug mode but fails in release mode.

这是我的代码片段,失败了:

Here's a snippet of my code where it fails:

LOADER->AllocBundle(&m_InitialContent);
while(!m_InitialContent.isReady())
{
    this->LoadingScreen();
}

AllocBundle()将加载m_InitialContent中包含的内容,并在完成时将其就绪状态设置为true.这是使用多线程实现的.

AllocBundle() will load the content contained in m_InitialContent and set it's ready status to true when it is done. This is implemented using multithreading.

this->LoadingScreen()应该呈现一个加载屏幕,但是目前尚未实现,因此该函数的主体为空.

this->LoadingScreen() should render a loading screen, however at the moment that is not implemented yet so the function has an empty body.

显然,这可能是导致错误的原因:如果我给函数LoadingScreen()一行代码:std::cout<<"Loading"<<std::endl;,则它将正常运行.

Apparently this might be the cause of the error: If I give the function LoadingScreen() one line of code: std::cout<<"Loading"<<std::endl; then it will run fine.

如果我不这样做,则代码将卡在while(!m_InitialContent.isReady())上,甚至不会跳转到方括号(this->LoadingScreen();)之间的代码.而且显然,它也不更新while语句中的表达式,因为它永远都停留在那里.

If I don't, then the code gets stuck at while(!m_InitialContent.isReady()) It never even jumps to the code between the brackets (this->LoadingScreen();). And apparently neither does it update the expression in the while statement because it stays stuck there forever.

有人有什么主意吗?如果是这样,可能是什么问题? 我完全不解.

Does anyone have any ideas what might be causing this? And if so, what might the problem be? I'm completely puzzled.

根据要求提供其他代码

ContentLoader成员:details::ContentBundleAllocator m_CBA;

member of ContentLoader: details::ContentBundleAllocator m_CBA;

    void ContentLoader::AllocBundle(ContentBundle* pBundle)
    {
        ASSERT(!(m_CBA.isRunning()), "ContentBundleAllocator is still busy");
        m_CBA.Alloc(pBundle, m_SystemInfo.dwNumberOfProcessors);
    }

void details::ContentBundleAllocator::Alloc(ContentBundle* pCB, UINT numThreads)
{
    m_bIsRunning = true;
    m_pCB = pCB;
    pCB->m_bIsReady = false;


    m_NumRunningThrds = numThreads;
    std::pair<UINT,HANDLE> p;
    for (UINT i = 0; i < numThreads; ++i)
    {
        p.second = (HANDLE)_beginthreadex(NULL,
                                          NULL,
                                          &details::ContentBundleAllocator::AllocBundle,
                                          this,
                                          NULL,&p.first);
        SetThreadPriority(p.second,THREAD_PRIORITY_HIGHEST);
        m_Threads.Insert(p);
    }
}

unsigned int __stdcall details::ContentBundleAllocator::AllocBundle(void* param)
{
//PREPARE
    ContentBundleAllocator* pCBA = (ContentBundleAllocator*)param;

//LOAD STUFF [collapsed for visibility+]

   //EXIT===========================================================================================================
        pCBA->m_NumRunningThrds -= 1;
        if (pCBA->m_NumRunningThrds == 0)
        {
            pCBA->m_bIsRunning = false;
            pCBA->m_pCB->m_bIsReady = true;
            pCBA->Clear();
    #ifdef DEBUG
            std::tcout << std::endl;
    #endif
            std::tcout<<_T("exiting allocation...")<<std::endl;
        }

    std::tcout<<_T("exiting thread...")<<std::endl;
    return 0;
}

bool isReady() const {return m_bIsReady;}

推荐答案

在Debug模式下编译代码时,编译器在后台进行了大量操作,以防止程序员犯下的许多错误导致应用程序崩溃.当您在Release中运行时,所有投注均关闭.如果您的代码不正确,那么在Release中崩溃的可能性要比在Debug中崩溃的可能性大.

When you compile your code in Debug mode, the compiler does a lot of stuff behind the scenes that prevents many mistakes made by the programmer from crashing the application. When you run in Release, all bets are off. If your code is not correct, you're much more likely to crash in Release than in Debug.

要检查的几件事:

  1. 确保所有变量均已正确初始化
  2. 确保您没有任何僵局或竞赛条件
  3. 确保您没有传递指向已被释放的本地对象的指针
  4. 确保您的字符串正确以NULL终止
  5. 不要出现意外的catch异常,然后继续运行,就好像什么都没发生一样.
  1. Make sure all variables are properly intialized
  2. Make sure you do not have any deadlocks or race conditions
  3. Make sure you aren't passing around pointers to local objects that have been deallocated
  4. Make sure your strings are properly NULL-terminated
  5. Don't catch exceptions that you're not expecting and then continue running as if nothing had happened.

这篇关于释放模式错误,但不在调试模式下的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆