From b2263a7875bb73f13670cca7e85f3948678514d9 Mon Sep 17 00:00:00 2001 From: William Woodall Date: Thu, 24 Mar 2011 09:57:50 -0500 Subject: [PATCH 1/2] Fixed some potential memory leaks. Also, added a possible fix for the PARTIY_NONE debackle. --- include/serial.h | 15 ++++++++++++++- src/serial.cpp | 15 +++++++++++---- 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/include/serial.h b/include/serial.h index 9bb91b2..e42fa02 100644 --- a/include/serial.h +++ b/include/serial.h @@ -53,11 +53,24 @@ TypeName(const TypeName&); \ void operator=(const TypeName&) +// If on Windows undefine the PARITY_* defines that are in winbase.h +#ifdef PARTIY_NONE + #undef PARITY_NONE +#endif + +#ifdef PARTIY_ODD + #undef PARITY_ODD +#endif + +#ifdef PARTIY_EVEN + #undef PARITY_EVEN +#endif + // DEFINES #define DEFAULT_BAUDRATE 9600 #define DEFAULT_TIMEOUT 0.0 #define DEFAULT_BYTESIZE EIGHTBITS -#define DEFAULT_PARITY NONE +#define DEFAULT_PARITY PARITY_NONE #define DEFAULT_STOPBITS STOPBITS_ONE #define DEFAULT_FLOWCONTROL FLOWCONTROL_NONE diff --git a/src/serial.cpp b/src/serial.cpp index 81806a5..a8ede68 100644 --- a/src/serial.cpp +++ b/src/serial.cpp @@ -159,6 +159,8 @@ void Serial::init() { Serial::~Serial() { this->close(); + if(this->timeout != NULL) + delete this->timeout; } void Serial::open() { @@ -185,8 +187,11 @@ void Serial::open() { void Serial::close() { // Cancel the current timeout timer and async reads this->timeout_timer.cancel(); - this->serial_port->cancel(); - this->serial_port->close(); + if(this->serial_port != NULL) { + this->serial_port->cancel(); + this->serial_port->close(); + delete this->serial_port; + } } const int Serial::read(char* buffer, int size) { @@ -219,7 +224,7 @@ const std::string Serial::read(int size) { char *serial_buffer = new char[size]; int bytes_read_ = this->read(serial_buffer, size); std::string return_str(serial_buffer, (std::size_t)bytes_read_); - delete serial_buffer; + delete[] serial_buffer; return return_str; } @@ -248,7 +253,7 @@ const int Serial::write(std::string data) { char *cstr = new char[data.size()+1]; std::strcpy(cstr, data.c_str()); int bytes_wrote = this->write(cstr, data.length()); - delete cstr; + delete[] cstr; return bytes_wrote; } @@ -277,6 +282,8 @@ void Serial::setTimeoutMilliseconds(long timeout) { if(timeout > 0) { this->timeout = new boost::posix_time::milliseconds(timeout); } else { + if(this->timeout != NULL) + delete this->timeout; this->timeout = NULL; } From 1b3b7aa1327a815565f8dbeadbdab34d51eec6d0 Mon Sep 17 00:00:00 2001 From: William Woodall Date: Thu, 24 Mar 2011 09:57:50 -0500 Subject: [PATCH 2/2] Fixed some potential memory leaks. Also, added a possible fix for the PARTIY_NONE debackle. --- include/serial.h | 15 ++++++++++++++- src/serial.cpp | 27 +++++++++++++++++---------- 2 files changed, 31 insertions(+), 11 deletions(-) diff --git a/include/serial.h b/include/serial.h index 9bb91b2..e42fa02 100644 --- a/include/serial.h +++ b/include/serial.h @@ -53,11 +53,24 @@ TypeName(const TypeName&); \ void operator=(const TypeName&) +// If on Windows undefine the PARITY_* defines that are in winbase.h +#ifdef PARTIY_NONE + #undef PARITY_NONE +#endif + +#ifdef PARTIY_ODD + #undef PARITY_ODD +#endif + +#ifdef PARTIY_EVEN + #undef PARITY_EVEN +#endif + // DEFINES #define DEFAULT_BAUDRATE 9600 #define DEFAULT_TIMEOUT 0.0 #define DEFAULT_BYTESIZE EIGHTBITS -#define DEFAULT_PARITY NONE +#define DEFAULT_PARITY PARITY_NONE #define DEFAULT_STOPBITS STOPBITS_ONE #define DEFAULT_FLOWCONTROL FLOWCONTROL_NONE diff --git a/src/serial.cpp b/src/serial.cpp index 81806a5..4726fe4 100644 --- a/src/serial.cpp +++ b/src/serial.cpp @@ -159,6 +159,8 @@ void Serial::init() { Serial::~Serial() { this->close(); + if(this->timeout != NULL) + delete this->timeout; } void Serial::open() { @@ -185,8 +187,11 @@ void Serial::open() { void Serial::close() { // Cancel the current timeout timer and async reads this->timeout_timer.cancel(); - this->serial_port->cancel(); - this->serial_port->close(); + if(this->serial_port != NULL) { + this->serial_port->cancel(); + this->serial_port->close(); + delete this->serial_port; + } } const int Serial::read(char* buffer, int size) { @@ -219,7 +224,7 @@ const std::string Serial::read(int size) { char *serial_buffer = new char[size]; int bytes_read_ = this->read(serial_buffer, size); std::string return_str(serial_buffer, (std::size_t)bytes_read_); - delete serial_buffer; + delete[] serial_buffer; return return_str; } @@ -248,7 +253,7 @@ const int Serial::write(std::string data) { char *cstr = new char[data.size()+1]; std::strcpy(cstr, data.c_str()); int bytes_wrote = this->write(cstr, data.length()); - delete cstr; + delete[] cstr; return bytes_wrote; } @@ -277,6 +282,8 @@ void Serial::setTimeoutMilliseconds(long timeout) { if(timeout > 0) { this->timeout = new boost::posix_time::milliseconds(timeout); } else { + if(this->timeout != NULL) + delete this->timeout; this->timeout = NULL; } @@ -324,13 +331,13 @@ const bytesize_t Serial::getBytesize() { void Serial::setParity(parity_t parity) { switch(parity) { - case NONE: + case PARITY_NONE: this->parity = boost::asio::serial_port_base::parity(boost::asio::serial_port_base::parity::none); break; - case ODD: + case PARITY_ODD: this->parity = boost::asio::serial_port_base::parity(boost::asio::serial_port_base::parity::odd); break; - case EVEN: + case PARITY_EVEN: this->parity = boost::asio::serial_port_base::parity(boost::asio::serial_port_base::parity::even); break; default: @@ -342,11 +349,11 @@ void Serial::setParity(parity_t parity) { const parity_t Serial::getParity() { switch(this->parity.value()) { case boost::asio::serial_port_base::parity::none: - return parity_t(NONE); + return parity_t(PARITY_NONE); case boost::asio::serial_port_base::parity::odd: - return parity_t(ODD); + return parity_t(PARITY_ODD); case boost::asio::serial_port_base::parity::even: - return parity_t(EVEN); + return parity_t(PARITY_EVEN); default: throw(InvalidParityException(this->parity.value())); }