Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

memory leak #35

Open
litaotao06051124 opened this issue Jul 10, 2024 · 0 comments
Open

memory leak #35

litaotao06051124 opened this issue Jul 10, 2024 · 0 comments

Comments

@litaotao06051124
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant