Skip to content

Latest commit

 

History

History
95 lines (78 loc) · 2.54 KB

c6335.md

File metadata and controls

95 lines (78 loc) · 2.54 KB
description title ms.date f1_keywords helpviewer_keywords ms.assetid
Learn more about: Warning C6335
Warning C6335
11/04/2016
C6335
LEAKING_PROCESS_HANDLE
__WARNING_LEAKING_PROCESS_HANDLE
C6335
f81c0859-3d82-4182-8bf1-6c3b76c7486f

Warning C6335

Leaking process information handle 'handlename'

Remarks

This warning indicates that the process information handles returned by the CreateProcess family of functions need to be closed using CloseHandle. Failure to do so will cause handle leaks.

Code analysis name: LEAKING_PROCESS_HANDLE

Example

The following code generates this warning:

#include <windows.h>
#include <stdio.h>

void f( )
{
    STARTUPINFO si;
    PROCESS_INFORMATION pi;

    ZeroMemory( &si, sizeof(si) );
    si.cb = sizeof(si);
    ZeroMemory( &pi, sizeof(pi) );

    // Start the child process.
    if( !CreateProcess( "C:\\WINDOWS\\system32\\calc.exe",
                        NULL,
                        NULL,
                        NULL,
                        FALSE,
                        0,
                        NULL,
                        NULL,
                        &si,    // Pointer to STARTUPINFO structure.
                        &pi ) ) // Pointer to PROCESS_INFORMATION
  {
    puts("Error");
    return;
  }
  // Wait until child process exits.
  WaitForSingleObject( pi.hProcess, INFINITE );
  CloseHandle( pi.hProcess );
}

To correct this warning, call CloseHandle (pi.``hThread) to close thread handle as shown in the following code:

#include <windows.h>
#include <stdio.h>

void f( )
{
    STARTUPINFO si;
    PROCESS_INFORMATION pi;

    ZeroMemory( &si, sizeof(si) );
    si.cb = sizeof(si);
    ZeroMemory( &pi, sizeof(pi) );

    // Start the child process.
    if( !CreateProcess( "C:\\WINDOWS\\system32\\calc.exe",
                        NULL,
                        NULL,
                        NULL,
                        FALSE,
                        0,
                        NULL,
                        NULL,
                        &si,    // Pointer to STARTUPINFO structure.
                        &pi ) ) // Pointer to PROCESS_INFORMATION
    {
      puts("Error");
      return;
    }

    // Wait until child process exits.
    WaitForSingleObject( pi.hProcess, INFINITE );
    CloseHandle( pi.hProcess );
    CloseHandle( pi.hThread );
}

For more information, see CreateProcess Function and CloseHandle Function.