Click here to Skip to main content
15,072,422 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.

C
#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");
    }
}
Posted
Updated 29-Oct-10 13:24pm
v2

1 solution

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900