您的位置:首页 > 大数据 > 人工智能

DllMain加载其他DLL造成的死锁问题及其解决办法

2013-11-21 10:41 274 查看
使用VS 2008新建一个MFC ActiveX工程,因为在工程里要用到GDI+。我习惯把初始化GDI+库的代码放在应用程序类的InitInstance函数,对应的销毁代码放在ExitInstance函数。具体如下:

先在应用程序类里定义一个数据成员:

[cpp] view plaincopy

ULONG_PTR m_gdiplusToken;

然后添加初始化GDI+库的代码和对应的销毁代码:

[cpp] view plaincopy

BOOL CImagePreviewXApp::InitInstance()
{
BOOL bInit = COleControlModule::InitInstance();
if (bInit)
{
// TODO: Add your own module initialization code here.
// Initialize GDI+ 的初始化代码,建议放在//InitInstance函数
GdiplusStartupInput gdiplusStartupInput;
GdiplusStartup(&m_gdiplusToken, &gdiplusStartupInput, NULL);
}
return bInit;
}
// CImagePreviewXApp::ExitInstance - DLL termination
int CImagePreviewXApp::ExitInstance()
{
// TODO: Add your own module termination code here.
GdiplusShutdown(m_gdiplusToken);
return COleControlModule::ExitInstance();
}

结果在编译时老是出现一个问题,就是编译时输出窗口:

1>Embedding manifest...
1>Microsoft (R) Windows (R) Resource Compiler Version 6.1.6723.1
1>Copyright (C) Microsoft Corporation. All rights reserved.
1>Registering output...

到了这里,VS 2008就像停滞一样,半天不反应,直到我取消生成。我以为是VS 2008的bug,因为我用VS 2010编译这个工程并无这个现象(这个实在有点令人奇怪!)。到论坛一问,蒋晟大侠告知:在DllMain的封装函数InitInstance中有加载其他DLL造成了死锁。

怎么解决这个问题呢?一种方法是写两个接口函数分别实现初始化GDI+库和对应的销毁功能。我懒得写两个接口函数,干脆把这个工作放在ActiveX控件类(派生自COleControl的那个类)的构造函数和析构函数里。

from:/article/2582421.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐