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

After the exception is caught here, does the program loop call, as in the example, cause a handle or memory leak #34

Open
jijieming opened this issue Jul 3, 2024 · 2 comments

Comments

@jijieming
Copy link

After the exception is caught here, does the program loop call, as in the example, cause a handle or memory leak:
int main(int /argc/, char */args/[])
{
try {
Win32_ComputerSystem computer = retrieveWmi<Win32_ComputerSystem>();
Win32_ComputerSystemProduct product = retrieveWmi<Win32_ComputerSystemProduct>();
SoftwareLicensingService liscense = retrieveWmi();
Win32_OperatingSystem os_info = retrieveWmi<Win32_OperatingSystem>();

        cout<<"Computername: "<<computer.Name<<" Domaind:"<<computer.Domain<<endl;
        cout<<"Product: "<<product.Name<<" UUID:"<<product.UUID<<endl;
        cout<<"Architecture: "<<os_info.OSArchitecture<<std::endl;
        cout<<"Roles: "<<endl;
        for(const string role : computer.Roles)
        {
            cout<<" - "<<role<<std::endl;
        }
        cout<<endl;
        cout<<"Installed services:"<<endl;
        for(const Win32_Service &service : retrieveAllWmi<Win32_Service>())
        {
            cout<<service.Caption<<endl;
        }
    } catch (const WmiException &ex) {
        cerr<<"Wmi error: "<<ex.errorMessage<<", Code: "<<ex.hexErrorCode()<<endl;
        return 1;
    }

    return 0;
}
@Thomas-Sparber
Copy link
Owner

Thanks for reporting the issue!

Do you know where the exception is thrown?

@Thomas-Sparber
Copy link
Owner

Thomas-Sparber commented Jul 3, 2024

I did a quick analysis, maybe line 421 in wmi.cpp needs to be copied to line 415 in wmi.cpp, so this:

....
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 (const WmiException &) {
	pServices->Release();
	pLocator->Release(); 
	CoUninitialize();
	throw;
}

pClassObject->Release();

pServices->Release();
pLocator->Release(); 
CoUninitialize();
...

becomes this:

...
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 (const WmiException &) {
	pClassObject->Release(); /* this line was added */
	pServices->Release();
	pLocator->Release(); 
	CoUninitialize();
	throw;
}

pClassObject->Release();

pServices->Release();
pLocator->Release(); 
CoUninitialize();
...

Can you please test and check if this fixes the issue?

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

2 participants