Description
Hello, I will use the following code to make multi-threaded concurrent calls:
void Wmi::query(const string& q, const string& p, WmiResult &out)
{
CoInitializeEx(0, COINIT_MULTITHREADED);
ULONG refCnt = 0;
LOGI("Wmi::query(" << q << ", " << p << ")");
IWbemLocator *pLocator = NULL;
IWbemServices *pServices = NULL;
IEnumWbemClassObject *pClassObject = NULL;
//Create the WBEM locator
try {
pLocator = createWbemLocator();
} catch (const WmiException &) {
LOGI("createWbemLocator exception! ");
CoUninitialize();
throw;
}
//Open connection to computer
try {
pServices = connect(pLocator, p);
} catch (WmiException &) {
LOGI("connect exception! ");
if (pServices)
{
refCnt = pServices->Release();
LOGI("pServices Release count: " << refCnt);
}
refCnt = pLocator->Release();
LOGI("pLocator Release count: " << refCnt);
CoUninitialize();
throw;
}
//Execute the query
try {
pClassObject = execute(pServices, q);
} catch (WmiException &) {
LOGI("execute exception! ");
if (pClassObject)
{
refCnt = pClassObject->Release();
LOGI("pClassObject Release count: " << refCnt);
}
refCnt = pServices->Release();
LOGI("pServices Release count: " << refCnt);
refCnt = pLocator->Release();
LOGI("pLocator Release count: " << refCnt);
CoUninitialize();
throw;
}
if (NULL == pClassObject)
{
LOGI("pClassObject is null! ");
refCnt = pServices->Release();
LOGI("pServices Release count: " << refCnt);
refCnt = pLocator->Release();
LOGI("pLocator Release count: " << refCnt);
CoUninitialize();
throw;
}
try {
std::size_t index = 0;
foreachObject(pClassObject, [&out,&index](IWbemClassObject *object)
{
foreachProperty(object, [&out,index](const wstring &name, const std::wstring &value)
{
out.set(index,name, value);
return true;
});
index++;
return true;
});
} catch (WmiException &) {
LOGI("foreachObject exception! ");
refCnt = pClassObject->Release();
LOGI("pClassObject Release count: " << refCnt);
refCnt = pServices->Release();
LOGI("pServices Release count: " << refCnt);
refCnt = pLocator->Release();
LOGI("pLocator Release count: " << refCnt);
CoUninitialize();
throw;
}
refCnt = pClassObject->Release();
LOGI("pClassObject Release count: " << refCnt);
refCnt = pServices->Release();
LOGI("pServices Release count: " << refCnt);
refCnt = pLocator->Release();
LOGI("pLocator Release count: " << refCnt);
CoUninitialize();
}
Thread 1 calls every 2 seconds: Win32.ComputerSystem computer=retrieveWmi<Win32.ComputerSystem>();
Thread 2 calls every 2 seconds: Win32.ComputerSystemProduct product=retrieveWmi<Win32.ComputerSystemProduct>();
Thread 3 calls every 2 seconds: Wins2OperatingSystem os_info=retrieveWmi();
Discovered a memory leak in the program