gpt4 book ai didi

c++ - 应用程序在 CCriticalSection::Lock 上中断

转载 作者:行者123 更新时间:2023-11-28 06:53:58 25 4
gpt4 key购买 nike

我正在将应用程序从 VC6 升级到 VS2010(旧版代码)。该应用程序在 VC6 中正常运行,但在将项目转换为 VS2010 后,我遇到了一些问题。退出应用程序时,程序会在尝试锁定进入临界区时中断。锁定计数通常从 -1(未锁定)到 -2(锁定)交替变化,但就在程序崩溃之前,锁定计数为 0。

g_RenderTargetCriticalSection.Lock();// Breaks here

if (g_RenderTargets.Lookup(this, pRenderTarget))
{
ASSERT_VALID(pRenderTarget);
g_RenderTargets.RemoveKey(this);
delete pRenderTarget;
}

g_RenderTargetCriticalSection.Unlock();

这是 CCriticalSection::Lock() 函数,其中 ::EnterCriticalSection(&m_sect); 失败。我发现很奇怪,在失败时,锁定计数从 0 变为 -4??

_AFXMT_INLINE BOOL (::CCriticalSection::Lock())
{
::EnterCriticalSection(&m_sect);

return TRUE;
}

如果有人遇到过类似的事情,将不胜感激。提前致谢。

最佳答案

注释表明这是一个文件范围对象析构函数顺序问题。有多种方法可以解决这个问题。由于我还没有看到其余代码,因此很难提供具体建议,但一个想法是将 CS 更改为存在于 shared_ptr 中,并让您的 CWnd 保留一个拷贝,这样它就不会不要过早销毁。例如:

std::shared_ptr<CCriticalSection> g_renderTargetCriticalSection(new CCriticalSection());

然后在你的窗口类中:

class CMyWindow : public CWnd
{
private:
std::shared_ptr<CCriticalSection> m_renderTargetCriticalSection;

public:
CMyWindow()
: m_renderTargetCriticalSection(g_renderTargetCriticalSection)
{
// ...
}

~CMyWindow()
{
// guaranteed to still be valid since our shared_ptr is keeping it alive
CSingleLock lock(m_renderTargetCriticalSection.get(), TRUE);
// ...
}

// ...
};

关于c++ - 应用程序在 CCriticalSection::Lock 上中断,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23410470/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com