Skip to content

Commit 763f308

Browse files
committed
Simplified open and close in case the pipe is already opened/closed
1 parent 0891699 commit 763f308

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

source/matplot/util/popen.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ int common_pipe::open(const std::string& cmd, char mode)
9696
int common_pipe::close(int *exit_code)
9797
{
9898
if (!opened())
99-
return report(EINVAL, "common_pipe::close");
99+
return 0; // nothing to do
100100
// Close the pipe to process:
101101
fclose(file_);
102102
file_ = nullptr;
@@ -128,6 +128,8 @@ int common_pipe::close(int *exit_code)
128128

129129
int common_pipe::open(const std::string &cmd, char mode)
130130
{
131+
if (opened())
132+
close(); // prevent resource leak
131133
constexpr auto READ = 0u;
132134
constexpr auto WRITE = 1u;
133135
int fd[2];
@@ -170,7 +172,7 @@ int common_pipe::open(const std::string &cmd, char mode)
170172
int common_pipe::close(int *exit_code)
171173
{
172174
if (!opened())
173-
return 0;
175+
return 0; // nothing to do
174176
::fclose(file_);
175177
file_ = nullptr;
176178
while (::waitpid(pid, exit_code, 0) == -1) {

0 commit comments

Comments
 (0)