C6335

警告 C6335:泄漏进程信息句柄 <handlename>

此警告意味着需要使用 CloseHandle 来关闭 CreateProcess 系列函数所返回的进程信息句柄。 否则,将导致句柄泄漏。

示例

下面的代码生成此警告:

#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) 来关闭线程句柄,如下面的代码所示:

#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 );
}

有关更多信息,请参见 CreateProcess Function(CreateProcess 函数)和 CloseHandle Function(CloseHandle 函数)。