IShellDispatch COM 接口,更具体地说是FolderItemVerbs对象,我遇到了一个非常奇怪的问题,这让我抓狂!
调用FolderItemVerbs::Release()后跟CoUninitialze()将导致崩溃。这显然是可重现的,但只会发生 10 次中的 1 次。
崩溃是“0xC0000005:访问冲突”错误。在循环中运行有问题的代码 100% 迟早会重现崩溃:-(
请看示例程序:
static int TestProc(const TCHAR *pcDirectoryName, const TCHAR *pcFileName)
{
int iSuccess = 0;
IShellDispatch *pShellDispatch = NULL;
Folder *pFolder = NULL; FolderItem *pItem = NULL;
FolderItemVerbs *pVerbs = NULL;
HRESULT hr = CoCreateInstance(CLSID_Shell, NULL, CLSCTX_INPROC_SERVER, IID_IShellDispatch, (void**)&pShellDispatch);
if(FAILED(hr) || (pShellDispatch == NULL))
{
iSuccess = -3;
return iSuccess;
}
variant_t vaDirectory(pcDirectoryName);
hr = pShellDispatch->NameSpace(vaDirectory, &pFolder);
if(FAILED(hr) || (pFolder == NULL))
{
iSuccess = -4;
pShellDispatch->Release();
return iSuccess;
}
variant_t vaFileName(pcFileName);
hr = pFolder->ParseName(vaFileName, &pItem);
if(FAILED(hr) || (pItem == NULL))
{
iSuccess = -5;
pFolder->Release();
pShellDispatch->Release();
return iSuccess;
}
hr = pItem->Verbs(&pVerbs);
if(FAILED(hr) || (pVerbs == NULL))
{
iSuccess = -6;
pItem->Release();
pFolder->Release();
pShellDispatch->Release();
return iSuccess;
}
/* Here we would do something with the FolderItemVerbs */
pVerbs->Release(); pVerbs = NULL; //If this line is commented out, we don't get a crash, but a massive memory leak!
pItem->Release(); pItem = NULL;
pFolder->Release(); pFolder = NULL;
pShellDispatch->Release(); pShellDispatch = NULL;
iSuccess = 1;
return iSuccess;
}
//-----------------------------------------------------------------------------
static unsigned __stdcall ThreadProc(void* pArguments)
{
HRESULT hr = CoInitialize(NULL);
if((hr == S_OK) || (hr == S_FALSE))
{
threadParam_t *params = (threadParam_t*) pArguments;
params->returnValue = TestProc(params->pcDirectoryName, params->pcFileName);
CoUninitialize();
}
else
{
if(threadParam_t *params = (threadParam_t*) pArguments)
{
params->returnValue = -10;
}
}
return EXIT_SUCCESS;
}
请在此处下载完整的示例代码:http: //pastie.org/private/0xsnajpia9lsmgnlf2afa
另请注意,我明确地追踪到崩溃到FolderItemVerbs,因为如果我从未创建过FolderItemVerbs对象,崩溃会立即消失。
此外,如果我在CoUninitialize()之前从未调用过“pVerbs->Release()”,那么崩溃也会消失,但这显然会导致大量内存泄漏。
另一个奇怪的事情是,如果我在调试器下运行程序,崩溃不会发生!但我可以运行程序,等待崩溃,然后让调试器处理崩溃。
不幸的是,我得到的堆栈跟踪并没有多大帮助:http: //pastie.org/private/cuwunlun2t5dc5lembpw
我不认为我在这里做错了什么。在过去的两天里,我一遍又一遍地检查了代码。所以这一切似乎都是 FolderItemVerbs 中的一个错误!
有没有人遇到过这个问题和/或可以确认这是 FolderItemVerbs 中的一个错误?另外,有没有解决这个问题的方法?
提前致谢 !!!