CreateProcessAsUser内存泄漏

时间:2012-12-31 11:01:00

标签: c++ windows memory-leaks windows-services

我有一个运行以下代码的服务。 每次击中CreateProcessAsUser时,内存都会增加4K(我可以在任务管理器中看到它)。当我在CreateProcess中使用时,这种情况不会发生。

为什么会发生这种情况以及如何释放这段记忆?

PWTS_SESSION_INFO pSessionInfo = 0;
DWORD dwCount = 0;
int dwSessionId = 0;

WTSEnumerateSessions(WTS_CURRENT_SERVER_HANDLE, 0, 1, &pSessionInfo, &dwCount);
for(DWORD i = 0; i < dwCount; ++i)
{
    WTS_SESSION_INFO si = pSessionInfo[i];
    if (WTSActive == si.State)
    {
        dwSessionId = si.SessionId;
        break;
    }
}
WTSFreeMemory(pSessionInfo);

HANDLE currentToken;
BOOL bRet = WTSQueryUserToken(dwSessionId, &currentToken);
if (bRet == false)
{
    return 0;
}

PROCESS_INFORMATION pi;
STARTUPINFO si;
memset(&si, 0, sizeof(si));
si.cb = sizeof(si);
if (CreateProcessAsUser(currentToken, "MY_EXE.exe", "MY_ARGS_STR", NULL, NULL, false, 0, NULL, NULL, &si, &pi))
{
    if (pi.hProcess != NULL)
    {
        CloseHandle(pi.hProcess);
    }
    if (pi.hThread != NULL)
    {
        CloseHandle(pi.hThread);
    }
}
CloseHandle(currentToken);

return 0;

0 个答案:

没有答案