I have used a tool called (PurifyPlus) to debug memory leaks issues in an application I wrote. I found out that it was leaking memory from a windows API call
CreateProcess()
. I have checked all the related documentation to this API and my code, there is no obvious reason why it should leak memory. The code is closing all the handles properly as per MSDN documentation.
To simplify the issue, I have created a very simply program that calls
CreateProcess
in a loop just to take everything else out of the equation and yet it is leaking memory. I can not figure out what is going on. Below is a simple code that produces the issue.
Please take a look at it and let me know what I am missing.
Thanks.
#include <windows.h>
int Exec(char* cmd)
{
STARTUPINFO startinfo;
PROCESS_INFORMATION processinfo;
memset (&startinfo,0,sizeof(startinfo));
startinfo.cb = sizeof (startinfo);
if (!CreateProcess(NULL, cmd, NULL, NULL, FALSE, 0, NULL, NULL,
&startinfo, &processinfo))
{
return -1;
}
DWORD exitCode;
if (WaitForSingleObject(processinfo.hProcess,INFINITE) == WAIT_FAILED)
{
exitCode = GetLastError();
}
else
{
if (!GetExitCodeProcess((HANDLE)processinfo.hProcess, &exitCode))
{
exitCode = GetLastError();
}
}
CloseHandle(processinfo.hThread);
CloseHandle(processinfo.hProcess);
return (int)exitCode;
}
void main (void)
{
for (int i =0; i<6; i++)
{
Exec ("cmd.exe /C echo hello");
}
}