Don't call CloseHandle() twice on Windows (as it causes crashes, or unexpected behavior). This would happen with the following test case:
ProcessHandle handle = Process.launch(...); handle.kill(); Then as handle gets out of scope, ~ProcessHandle would call CloseHandle() on an already closed handle.
This commit is contained in:
Родитель
e06fec0e24
Коммит
c59f8865ac
|
@ -237,7 +237,7 @@ public:
|
|||
/// Waits for the process specified by handle to terminate
|
||||
/// and returns the exit code of the process.
|
||||
|
||||
static void kill(const ProcessHandle& handle);
|
||||
static void kill(ProcessHandle& handle);
|
||||
/// Kills the process specified by handle.
|
||||
///
|
||||
/// This is preferable on Windows where process IDs
|
||||
|
|
|
@ -84,7 +84,7 @@ public:
|
|||
Pipe* outPipe,
|
||||
Pipe* errPipe,
|
||||
const EnvImpl& env);
|
||||
static void killImpl(const ProcessHandleImpl& handle);
|
||||
static void killImpl(ProcessHandleImpl& handle);
|
||||
static void killImpl(PIDImpl pid);
|
||||
static void requestTerminationImpl(PIDImpl pid);
|
||||
|
||||
|
|
|
@ -85,7 +85,7 @@ public:
|
|||
Pipe* errPipe,
|
||||
const EnvImpl& env);
|
||||
static int waitImpl(PIDImpl pid);
|
||||
static void killImpl(const ProcessHandleImpl& handle);
|
||||
static void killImpl(ProcessHandleImpl& handle);
|
||||
static void killImpl(PIDImpl pid);
|
||||
static void requestTerminationImpl(PIDImpl pid);
|
||||
};
|
||||
|
|
|
@ -86,7 +86,7 @@ public:
|
|||
Pipe* outPipe,
|
||||
Pipe* errPipe,
|
||||
const EnvImpl& env);
|
||||
static void killImpl(const ProcessHandleImpl& handle);
|
||||
static void killImpl(ProcessHandleImpl& handle);
|
||||
static void killImpl(PIDImpl pid);
|
||||
static void requestTerminationImpl(PIDImpl pid);
|
||||
};
|
||||
|
|
|
@ -62,7 +62,8 @@ public:
|
|||
UInt32 id() const;
|
||||
HANDLE process() const;
|
||||
int wait() const;
|
||||
|
||||
void closeHandle();
|
||||
|
||||
private:
|
||||
HANDLE _hProcess;
|
||||
UInt32 _pid;
|
||||
|
@ -89,7 +90,7 @@ public:
|
|||
Pipe* outPipe,
|
||||
Pipe* errPipe,
|
||||
const EnvImpl& env);
|
||||
static void killImpl(const ProcessHandleImpl& handle);
|
||||
static void killImpl(ProcessHandleImpl& handle);
|
||||
static void killImpl(PIDImpl pid);
|
||||
static void requestTerminationImpl(PIDImpl pid);
|
||||
static std::string terminationEventName(PIDImpl pid);
|
||||
|
|
|
@ -62,6 +62,7 @@ public:
|
|||
UInt32 id() const;
|
||||
HANDLE process() const;
|
||||
int wait() const;
|
||||
void closeHandle();
|
||||
|
||||
private:
|
||||
HANDLE _hProcess;
|
||||
|
@ -89,7 +90,7 @@ public:
|
|||
Pipe* outPipe,
|
||||
Pipe* errPipe,
|
||||
const EnvImpl& env);
|
||||
static void killImpl(const ProcessHandleImpl& handle);
|
||||
static void killImpl(ProcessHandleImpl& handle);
|
||||
static void killImpl(PIDImpl pid);
|
||||
static void requestTerminationImpl(PIDImpl pid);
|
||||
static std::string terminationEventName(PIDImpl pid);
|
||||
|
|
|
@ -62,7 +62,8 @@ public:
|
|||
UInt32 id() const;
|
||||
HANDLE process() const;
|
||||
int wait() const;
|
||||
|
||||
void closeHandle();
|
||||
|
||||
private:
|
||||
HANDLE _hProcess;
|
||||
UInt32 _pid;
|
||||
|
@ -89,7 +90,7 @@ public:
|
|||
Pipe* outPipe,
|
||||
Pipe* errPipe,
|
||||
const EnvImpl& env);
|
||||
static void killImpl(const ProcessHandleImpl& handle);
|
||||
static void killImpl(ProcessHandleImpl& handle);
|
||||
static void killImpl(PIDImpl pid);
|
||||
static void requestTerminationImpl(PIDImpl pid);
|
||||
static std::string terminationEventName(PIDImpl pid);
|
||||
|
|
|
@ -200,7 +200,7 @@ int Process::wait(const ProcessHandle& handle)
|
|||
}
|
||||
|
||||
|
||||
void Process::kill(const ProcessHandle& handle)
|
||||
void Process::kill(ProcessHandle& handle)
|
||||
{
|
||||
killImpl(*handle._pImpl);
|
||||
}
|
||||
|
|
|
@ -218,7 +218,7 @@ ProcessHandleImpl* ProcessImpl::launchByForkExecImpl(const std::string& command,
|
|||
}
|
||||
|
||||
|
||||
void ProcessImpl::killImpl(const ProcessHandleImpl& handle)
|
||||
void ProcessImpl::killImpl(ProcessHandleImpl& handle)
|
||||
{
|
||||
killImpl(handle.id());
|
||||
}
|
||||
|
|
|
@ -137,7 +137,7 @@ ProcessHandleImpl* ProcessImpl::launchImpl(const std::string& command, const Arg
|
|||
}
|
||||
|
||||
|
||||
void ProcessImpl::killImpl(const ProcessHandleImpl& handle)
|
||||
void ProcessImpl::killImpl(ProcessHandleImpl& handle)
|
||||
{
|
||||
killImpl(handle.id());
|
||||
}
|
||||
|
|
|
@ -89,7 +89,7 @@ ProcessHandleImpl* ProcessImpl::launchImpl(const std::string& command, const Arg
|
|||
}
|
||||
|
||||
|
||||
void ProcessImpl::killImpl(const ProcessHandleImpl& handle)
|
||||
void ProcessImpl::killImpl(ProcessHandleImpl& handle)
|
||||
{
|
||||
throw Poco::NotImplementedException("Process::kill()");
|
||||
}
|
||||
|
|
|
@ -56,9 +56,17 @@ ProcessHandleImpl::ProcessHandleImpl(HANDLE hProcess, UInt32 pid):
|
|||
|
||||
ProcessHandleImpl::~ProcessHandleImpl()
|
||||
{
|
||||
CloseHandle(_hProcess);
|
||||
closeHandle();
|
||||
}
|
||||
|
||||
void ProcessHandleImpl::closeHandle()
|
||||
{
|
||||
if (_hProcess)
|
||||
{
|
||||
CloseHandle(_hProcess);
|
||||
_hProcess = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
UInt32 ProcessHandleImpl::id() const
|
||||
{
|
||||
|
@ -228,17 +236,19 @@ ProcessHandleImpl* ProcessImpl::launchImpl(const std::string& command, const Arg
|
|||
}
|
||||
|
||||
|
||||
void ProcessImpl::killImpl(const ProcessHandleImpl& handle)
|
||||
void ProcessImpl::killImpl(ProcessHandleImpl& handle)
|
||||
{
|
||||
if (TerminateProcess(handle.process(), 0) == 0)
|
||||
if (handle.process())
|
||||
{
|
||||
CloseHandle(handle.process());
|
||||
throw SystemException("cannot kill process");
|
||||
if (TerminateProcess(handle.process(), 0) == 0)
|
||||
{
|
||||
handle.closeHandle();
|
||||
throw SystemException("cannot kill process");
|
||||
}
|
||||
handle.closeHandle();
|
||||
}
|
||||
CloseHandle(handle.process());
|
||||
}
|
||||
|
||||
|
||||
void ProcessImpl::killImpl(PIDImpl pid)
|
||||
{
|
||||
HANDLE hProc = OpenProcess(PROCESS_TERMINATE, FALSE, pid);
|
||||
|
|
|
@ -57,9 +57,17 @@ ProcessHandleImpl::ProcessHandleImpl(HANDLE hProcess, UInt32 pid):
|
|||
|
||||
ProcessHandleImpl::~ProcessHandleImpl()
|
||||
{
|
||||
CloseHandle(_hProcess);
|
||||
closeHandle();
|
||||
}
|
||||
|
||||
void ProcessHandleImpl::closeHandle()
|
||||
{
|
||||
if (_hProcess)
|
||||
{
|
||||
CloseHandle(_hProcess);
|
||||
_hProcess = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
UInt32 ProcessHandleImpl::id() const
|
||||
{
|
||||
|
@ -234,17 +242,19 @@ ProcessHandleImpl* ProcessImpl::launchImpl(const std::string& command, const Arg
|
|||
}
|
||||
|
||||
|
||||
void ProcessImpl::killImpl(const ProcessHandleImpl& handle)
|
||||
void ProcessImpl::killImpl(ProcessHandleImpl& handle)
|
||||
{
|
||||
if (TerminateProcess(handle.process(), 0) == 0)
|
||||
if (handle.process())
|
||||
{
|
||||
CloseHandle(handle.process());
|
||||
throw SystemException("cannot kill process");
|
||||
if (TerminateProcess(handle.process(), 0) == 0)
|
||||
{
|
||||
handle.closeHandle();
|
||||
throw SystemException("cannot kill process");
|
||||
}
|
||||
handle.closeHandle();
|
||||
}
|
||||
CloseHandle(handle.process());
|
||||
}
|
||||
|
||||
|
||||
void ProcessImpl::killImpl(PIDImpl pid)
|
||||
{
|
||||
HANDLE hProc = OpenProcess(PROCESS_TERMINATE, FALSE, pid);
|
||||
|
|
|
@ -57,9 +57,17 @@ ProcessHandleImpl::ProcessHandleImpl(HANDLE hProcess, UInt32 pid):
|
|||
|
||||
ProcessHandleImpl::~ProcessHandleImpl()
|
||||
{
|
||||
CloseHandle(_hProcess);
|
||||
closeHandle();
|
||||
}
|
||||
|
||||
void ProcessHandleImpl::closeHandle()
|
||||
{
|
||||
if (_hProcess)
|
||||
{
|
||||
CloseHandle(_hProcess);
|
||||
_hProcess = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
UInt32 ProcessHandleImpl::id() const
|
||||
{
|
||||
|
@ -158,14 +166,17 @@ ProcessHandleImpl* ProcessImpl::launchImpl(const std::string& command, const Arg
|
|||
}
|
||||
|
||||
|
||||
void ProcessImpl::killImpl(const ProcessHandleImpl& handle)
|
||||
void ProcessImpl::killImpl(ProcessHandleImpl& handle)
|
||||
{
|
||||
if (TerminateProcess(handle.process(), 0) == 0)
|
||||
if (handle.process())
|
||||
{
|
||||
CloseHandle(handle.process());
|
||||
throw SystemException("cannot kill process");
|
||||
if (TerminateProcess(handle.process(), 0) == 0)
|
||||
{
|
||||
handle.closeHandle();
|
||||
throw SystemException("cannot kill process");
|
||||
}
|
||||
handle.closeHandle();
|
||||
}
|
||||
CloseHandle(handle.process());
|
||||
}
|
||||
|
||||
|
||||
|
|
Загрузка…
Ссылка в новой задаче