diff options
author | Brian Orr <brian.orr@gmail.com> | 2017-08-16 10:08:07 -0700 |
---|---|---|
committer | Brian Orr <brian.orr@gmail.com> | 2017-08-16 10:08:07 -0700 |
commit | 643fee9496b84061a01042c4926e4b5e98f1e498 (patch) | |
tree | 53de3c0ae8e8de3da1417d065b96e9317ac60159 | |
parent | 495d15c0f7dcc458a156b23a226216d574bf517a (diff) |
Swap new/delete memory management with boost::shared_ptr
-rw-r--r-- | gr-blocks/lib/tcp_connection.cc | 9 | ||||
-rw-r--r-- | gr-blocks/lib/tcp_connection.h | 6 |
2 files changed, 7 insertions, 8 deletions
diff --git a/gr-blocks/lib/tcp_connection.cc b/gr-blocks/lib/tcp_connection.cc index 6980ddadb0..b28accccf7 100644 --- a/gr-blocks/lib/tcp_connection.cc +++ b/gr-blocks/lib/tcp_connection.cc @@ -55,12 +55,11 @@ namespace gr { { size_t len = pmt::blob_length(vector); - // Asio async_write() requires the buffer to remain valid until the handler is called. It will be - // deleted in tcp_connection::handle_write. - char* txbuf = new char[len]; + // Asio async_write() requires the buffer to remain valid until the handler is called. + boost::shared_ptr<char[]> txbuf(new char[len]); size_t temp = 0; - memcpy(txbuf, pmt::uniform_vector_elements(vector, temp), len); + memcpy(txbuf.get(), pmt::uniform_vector_elements(vector, temp), len); size_t offset = 0; while (offset < len) { @@ -68,7 +67,7 @@ namespace gr { // FIXME: Note that this has the effect of breaking a large PDU into several smaller PDUs, each // containing <= MTU bytes. Is this the desired behavior? size_t send_len = std::min((len - offset), d_buf.size()); - boost::asio::async_write(d_socket, boost::asio::buffer(txbuf + offset, send_len), + boost::asio::async_write(d_socket, boost::asio::buffer(txbuf.get() + offset, send_len), boost::bind(&tcp_connection::handle_write, this, txbuf, boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred)); diff --git a/gr-blocks/lib/tcp_connection.h b/gr-blocks/lib/tcp_connection.h index f45650577b..eb4c0df285 100644 --- a/gr-blocks/lib/tcp_connection.h +++ b/gr-blocks/lib/tcp_connection.h @@ -25,6 +25,7 @@ #include <boost/array.hpp> #include <boost/asio.hpp> +#include <boost/shared_ptr.hpp> #include <pmt/pmt.h> namespace gr { @@ -53,9 +54,8 @@ namespace gr { void start(gr::basic_block *block); void send(pmt::pmt_t vector); void handle_read(const boost::system::error_code& error, size_t bytes_transferred); - void handle_write(char* txbuf, const boost::system::error_code& error, size_t bytes_transferred) { - delete[] txbuf; - } + void handle_write(boost::shared_ptr<char[]> txbuf, const boost::system::error_code& error, + size_t bytes_transferred) { } }; } /* namespace blocks */ |