From 6bdab62358d6a6fb93e7dfeab89c6c3b9abd031a Mon Sep 17 00:00:00 2001 From: ridiculousfish Date: Sun, 22 Jan 2017 00:44:04 -0800 Subject: [PATCH] Make io_buffer_t::create return a shared_ptr Eliminates some manual memory management --- src/exec.cpp | 4 ++-- src/io.cpp | 7 +++---- src/io.h | 2 +- 3 files changed, 6 insertions(+), 7 deletions(-) diff --git a/src/exec.cpp b/src/exec.cpp index 63d90eb0f..4dd046488 100644 --- a/src/exec.cpp +++ b/src/exec.cpp @@ -642,7 +642,7 @@ void exec_job(parser_t &parser, job_t *j) { if (p->next) { // Be careful to handle failure, e.g. too many open fds. - block_output_io_buffer.reset(io_buffer_t::create(STDOUT_FILENO, all_ios)); + block_output_io_buffer = io_buffer_t::create(STDOUT_FILENO, all_ios); if (block_output_io_buffer.get() == NULL) { exec_error = true; job_mark_process_as_failed(j, p); @@ -668,7 +668,7 @@ void exec_job(parser_t &parser, job_t *j) { case INTERNAL_BLOCK_NODE: { if (p->next) { - block_output_io_buffer.reset(io_buffer_t::create(STDOUT_FILENO, all_ios)); + block_output_io_buffer = io_buffer_t::create(STDOUT_FILENO, all_ios); if (block_output_io_buffer.get() == NULL) { // We failed (e.g. no more fds could be created). exec_error = true; diff --git a/src/io.cpp b/src/io.cpp index 5cdc5c336..22af1195e 100644 --- a/src/io.cpp +++ b/src/io.cpp @@ -76,10 +76,10 @@ bool io_buffer_t::avoid_conflicts_with_io_chain(const io_chain_t &ios) { return result; } -io_buffer_t *io_buffer_t::create(int fd, const io_chain_t &conflicts) { +shared_ptr io_buffer_t::create(int fd, const io_chain_t &conflicts) { bool success = true; assert(fd >= 0); - io_buffer_t *buffer_redirect = new io_buffer_t(fd); + shared_ptr buffer_redirect(new io_buffer_t(fd)); if (exec_pipe(buffer_redirect->pipe_fd) == -1) { debug(1, PIPE_ERROR); @@ -95,8 +95,7 @@ io_buffer_t *io_buffer_t::create(int fd, const io_chain_t &conflicts) { } if (!success) { - delete buffer_redirect; - buffer_redirect = NULL; + buffer_redirect.reset(); } return buffer_redirect; } diff --git a/src/io.h b/src/io.h index f8149a818..25a277cd7 100644 --- a/src/io.h +++ b/src/io.h @@ -133,7 +133,7 @@ class io_buffer_t : public io_pipe_t { /// \param fd the fd that will be mapped in the child process, typically STDOUT_FILENO /// \param conflicts A set of IO redirections. The function ensures that any pipe it makes does /// not conflict with an fd redirection in this list. - static io_buffer_t *create(int fd, const io_chain_t &conflicts); + static shared_ptr create(int fd, const io_chain_t &conflicts); }; class io_chain_t : public std::vector > {