gpt4 book ai didi

c++ - 为什么要发布 DC()?

转载 作者:行者123 更新时间:2023-11-30 03:54:39 25 4
gpt4 key购买 nike

我正在开发 MFC 应用程序,有时我在空闲处理循环期间遇到了一些奇怪的访问冲突错误。

经过一些研究,我发现这可能是由未发布的 DC 引起的。在每次调用 GetDC() 并且错误停止出现后,我都调用了 ReleaseDC() 函数。

我不明白的是为什么我必须调用 ReleaseDC()?.例如:

void CMainFrame::OnUpdateStatusLODLevel(CCmdUI *pCmdUI)
{
pCmdUI->Enable();

RenderingStatistics Stats = theApp.xAM->GetRenderingStatistics();

std::wstring wsText;

wsText = L"LOD: ";
wsText += std::to_wstring(Stats.uiCurrentLOD);

pCmdUI->SetText(wsText.c_str());

CDC* dc = m_wndStatusBar.GetDC();

if (dc){
CSize size = dc->GetTextExtent(wsText.c_str());
m_wndStatusBar.SetPaneWidth(m_wndStatusBar.CommandToIndex(ID_STATUS_BAR_LOD_LEVEL), size.cx);

ReleaseDC(dc); // without this call Access Violation errors appears
}
}

最佳答案

克里斯说:

If you take a DC, you should give it back when you're done. It's as simple as that.

他/她是对的。

一些程序员说:

Resources on a computer are not infinite, that includes things such as handles, device-contexts, files etc. If you just keep on allocating resource and not releasing them, the system will sooner or later run out of that resource.

他/她也是对的。

你应该听他们的。

其他应用程序和进程,甚至当前进程和应用程序中的其他线程,将无法使用 GetDCGetWindowDC 检索窗口的设备上下文,直到您的应用程序调用/调用 ReleaseDC

关于c++ - 为什么要发布 DC()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29433674/

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