Just wasted a few hours of my life with a seemingly
trivial segment of code. Spot the error:
HRESULT hrInit = CoInitialize(NULL);
CUserProfileWebService webService;
webService.Execute();
if (SUCCEEDED(hrInit))
CoUninitialize();
I was getting an Access Violation when the application exited, which means I was trying to access memory that no longer belonged to me. Turns out, the problem was due to scope. The "
webService" object doesn't fall out of scope until
after CoUninitialize has been called, so it doesn't know to clean itself up until it's too late. The fix is to enclose the webService variable in it's own scope, such as the following:
HRESULT hrInit = CoInitialize(NULL);
{
CUserProfileWebService webService;
webService.Execute();
}
if (SUCCEEDED(hrInit))
CoUninitialize();
In this instance, “
webService” will clean itself up immediately after the
} curly brace. Of course, it took
forever to narrow it down to these five lines of code, at which point I realized my mistake. *sigh*