I am unaware of a good way to check this myself, so I am asking here. I am pretty new to smart pointers so my question is: In this code example, will all handlers be automatically closed? Is there something else that should be done?
#include <stdio.h>
#include <windows.h>
#include <fileapi.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <memory>
struct Deleter
{
void operator()(HANDLE* h)
{
CloseHandle(*h);
}
};
void Close(HANDLE* handle)
{
CloseHandle(*handle);
}
void helper()
{
std::wstring str = L"Project2.exe 30 30";
STARTUPINFO si = { sizeof(si) };
PROCESS_INFORMATION pi;
CreateProcess(NULL, str.data(), nullptr, nullptr, false, NORMAL_PRIORITY_CLASS, nullptr, nullptr, &si, &pi);
STARTUPINFO li = { sizeof(si) };
PROCESS_INFORMATION ki;
CreateProcess(NULL, str.data(), nullptr, nullptr, false, NORMAL_PRIORITY_CLASS, nullptr, nullptr, &li, &ki);
std::unique_ptr<HANDLE, Deleter> xx = std::unique_ptr<HANDLE, Deleter>(&pi.hProcess);
std::unique_ptr<HANDLE, Deleter> yy(&pi.hThread);
std::shared_ptr<HANDLE> zz(&ki.hProcess, Close);
std::shared_ptr<HANDLE> ww(&ki.hThread, Close);
}
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pCmdLine, int nCmdShow)
{
helper();
}
Unfortunately, using CHandle is not possible for me.