I am trying to read from a serial port (actually a USB port but using ftdi_sio).
I am using the boost library. My program reads the port every 10ms and everything seems to be fine but if I let it run for 20seconds to a minute or so, I get the following error :
*** glibc detected *** ./test2-boost: double free or corruption (fasttop): 0x080ed098 ***
and it stops. test2-boost is the name of my project in code::blocks.
I have read that you could do something like
Code: Select all
export MALLOC_CHECK_=0
Any idea how to deal with this error ?
My code is as follows:
Code: Select all
#include <string>
#include <time.h>
#include <iostream>
#include <unistd.h>
#include <sys/time.h>
#include <deque>
#include <iostream>
#include <boost/bind.hpp>
#include <boost/asio.hpp>
#include <boost/asio/serial_port.hpp>
#include <boost/thread.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/date_time/posix_time/posix_time_types.hpp>
using namespace std;
class minicom_client
{
public:
minicom_client(boost::asio::io_service& io_service, unsigned int baud, const string& device)
: active_(true),
io_service_(io_service),
serialPort(io_service, device)
{
if (!serialPort.is_open())
{
cerr << "Failed to open serial port\n";
return;
}
boost::asio::serial_port_base::baud_rate baud_option(baud);
serialPort.set_option(baud_option); // set the baud rate after the port has been opened
read_start();
}
string lire()
{
string temp = recu;
recu.clear();
return temp;
}
void close() // call the do_close function via the io service in the other thread
{
io_service_.post(boost::bind(&minicom_client::do_close, this, boost::system::error_code()));
}
bool active() // return true if the socket is still active
{
return active_;
}
private:
static const int max_read_length = 512; // maximum amount of data to read in one operation
void read_start(void)
{ // Start an asynchronous read and call read_complete when it completes or fails
serialPort.async_read_some(boost::asio::buffer(read_msg_, max_read_length),
boost::bind(&minicom_client::read_complete,
this,
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
}
void read_complete(const boost::system::error_code& error, size_t bytes_transferred)
{ // the asynchronous read operation has now completed or failed and returned an error
if (!error)
{ // read completed, so process the data
recu.append(read_msg_, bytes_transferred);
read_start(); // start waiting for another asynchronous read again
}
else
do_close(error);
}
void do_close(const boost::system::error_code& error)
{ // something has gone wrong, so close the socket & make this object inactive
if (error == boost::asio::error::operation_aborted) // if this call is the result of a timer cancel()
return; // ignore it because the connection cancelled the timer
if (error)
cerr << "Error: " << error.message() << endl; // show the error message
else
cout << "Error: Connection did not succeed.\n";
cout << "Press Enter to exit\n";
serialPort.close();
active_ = false;
}
private:
bool active_; // remains true while this object is still operating
boost::asio::io_service& io_service_; // the main IO service that runs this connection
boost::asio::serial_port serialPort; // the serial port this instance is connected to
char read_msg_[max_read_length]; // data read from the socket
std::string recu;
deque<char> write_msgs_; // buffered write data
};
using namespace std;
int main(int argc, char* argv[])
{
struct timeval tv1, tv2;
struct timezone tz;
struct timespec ts, rem;
int diff;
try
{
if (argc != 3)
{
cerr << "Usage: minicom <baud> <device>\n";
return 1;
}
ts.tv_sec = 0;
ts.tv_nsec = 10000000;
rem = ts;
boost::asio::io_service io_service;
// define an instance of the main class of this program
minicom_client c(io_service, boost::lexical_cast<unsigned int>(argv[1]), argv[2]);
// run the IO service as a separate thread, so the main thread can block on standard input
boost::thread t(boost::bind(&boost::asio::io_service::run, &io_service));
while (c.active()) // check the internal state of the connection to make sure it's still running
{
gettimeofday(&tv1, &tz);
cout << tv1.tv_sec << " s " << tv1.tv_usec << endl;
cout << c.lire() << endl;
nanosleep(&ts, &rem);
}
c.close(); // close the minicom client connection
t.join(); // wait for the IO service thread to close
}
catch (exception& e)
{
cerr << "Exception: " << e.what() << "\n";
}
return 0;
}
Code: Select all
[more of it]
1271454503 s 181315
AAAAAAAAAAA
1271454503 s 191429
AAAAAAAA
1271454503 s 201558
AAAAAAAAAAAA
1271454503 s 211677
*** glibc detected *** ./test2-boost: double free or corruption (fasttop): 0x080ed098 ***
======= Backtrace: =========
/lib/libc.so.6[0xb753c40e]
/lib/libc.so.6(cfree+0xa9)[0xb753dbdd]
/usr/lib/libstdc++.so.6(_ZdlPv+0x23)[0xb76d53a3]
/usr/lib/libstdc++.so.6(_ZNSs4_Rep10_M_destroyERKSaIcE+0x1c)[0xb76b31ec]
/usr/lib/libstdc++.so.6(_ZNSsD1Ev+0x52)[0xb76b5012]
./test2-boost[0x805d574]
/lib/libc.so.6(__libc_start_main+0x12e)[0xb74ff20e]
./test2-boost(pthread_cancel+0x6d)[0x804ae21]
======= Memory map: ========
08048000-08074000 r-xp 00000000 00:10 58015 /root/my-documents/test2-boost/bin/Debug/test2-boost
08074000-08075000 rwxp 0002c000 00:10 58015 /root/my-documents/test2-boost/bin/Debug/test2-boost
080ed000-0810e000 rwxp 00000000 00:00 0 [heap]
b6b00000-b6b21000 rwxp 00000000 00:00 0
b6b21000-b6c00000 ---p 00000000 00:00 0
b6ce6000-b6ce7000 rwxp 00000000 00:00 0
b6ce7000-b6ce8000 ---p 00000000 00:00 0
b6ce8000-b74e9000 rwxp 00000000 00:00 0
b74e9000-b75de000 r-xp 00000000 00:10 161 /lib/libc-2.6.1.so
b75de000-b75df000 r-xp 000f5000 00:10 161 /lib/libc-2.6.1.so
b75df000-b75e1000 rwxp 000f6000 00:10 161 /lib/libc-2.6.1.so
b75e1000-b75e4000 rwxp 00000000 00:00 0
b75e4000-b75f5000 r-xp 00000000 00:10 219 /lib/libpthread-2.6.1.so
b75f5000-b75f7000 rwxp 00010000 00:10 219 /lib/libpthread-2.6.1.so
b75f7000-b75f9000 rwxp 00000000 00:00 0
b75f9000-b7603000 r-xp 00000000 00:10 1558 /usr/lib/libgcc_s.so.1
b7603000-b7604000 rwxp 00009000 00:10 1558 /usr/lib/libgcc_s.so.1
b7604000-b7624000 r-xp 00000000 00:10 906 /lib/libm-2.6.1.so
b7624000-b7626000 rwxp 00020000 00:10 906 /lib/libm-2.6.1.so
b7626000-b7702000 r-xp 00000000 00:10 1152 /usr/lib/libstdc++.so.6.0.9
b7702000-b7706000 r-xp 000db000 00:10 1152 /usr/lib/libstdc++.so.6.0.9
b7706000-b7707000 rwxp 000df000 00:10 1152 /usr/lib/libstdc++.so.6.0.9
b7707000-b770e000 rwxp 00000000 00:00 0
b770e000-b770f000 r-xp 00000000 00:00 0 [vdso]
b770f000-b7724000 r-xp 00000000 00:10 157 /lib/ld-2.6.1.so
b7724000-b7726000 rwxp 00014000 00:10 157 /lib/ld-2.6.1.so
bfcd5000-bfcea000 rw-p 00000000 00:00 0 [stack]
Aborted