From 2442ba209cf2e049394c79796effd216d55467a6 Mon Sep 17 00:00:00 2001 From: Yutaka Kondo Date: Tue, 20 Aug 2019 02:32:00 +0900 Subject: [PATCH] Use shared pointer when binding callback for serial read (#38) * Resolves an issue with ROS Melodic on 18.04. --- include/create/serial.h | 3 ++- src/serial.cpp | 4 ++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/include/create/serial.h b/include/create/serial.h index 9dd9bc3..28b5f69 100644 --- a/include/create/serial.h +++ b/include/create/serial.h @@ -40,13 +40,14 @@ POSSIBILITY OF SUCH DAMAGE. #include #include #include +#include #include "create/data.h" #include "create/types.h" #include "create/util.h" namespace create { - class Serial { + class Serial : public boost::enable_shared_from_this { protected: boost::asio::io_service io; diff --git a/src/serial.cpp b/src/serial.cpp index 3894b9e..12876aa 100644 --- a/src/serial.cpp +++ b/src/serial.cpp @@ -72,7 +72,7 @@ namespace create { // Start continuously reading one byte at a time boost::asio::async_read(port, boost::asio::buffer(&byteRead, 1), - boost::bind(&Serial::onData, this, _1, _2)); + boost::bind(&Serial::onData, shared_from_this(), _1, _2)); ioThread = boost::thread(boost::bind(&boost::asio::io_service::run, &io)); @@ -145,7 +145,7 @@ namespace create { // Read the next byte boost::asio::async_read(port, boost::asio::buffer(&byteRead, 1), - boost::bind(&Serial::onData, this, _1, _2)); + boost::bind(&Serial::onData, shared_from_this(), _1, _2)); } bool Serial::send(const uint8_t* bytes, unsigned int numBytes) {