changed API for setting network parameter to remove memory leak, fixed lock issues in streaming print outs to gui

This commit is contained in:
Dhanya Maliakal 2017-06-19 14:19:30 +02:00
parent 47e5504203
commit 2ec1e08081
3 changed files with 36 additions and 25 deletions

View File

@ -17,6 +17,7 @@
#include <QString> #include <QString>
#include <QCustomEvent> #include <QCustomEvent>
#include <iostream> #include <iostream>
#include <streambuf> #include <streambuf>
#include <string> #include <string>
@ -47,6 +48,7 @@ class qDebugStream : public basic_streambuf<char> {
public: public:
qDebugStream(ostream &stream, QWidget* w) : m_stream(stream), log_window(w) { qDebugStream(ostream &stream, QWidget* w) : m_stream(stream), log_window(w) {
mutex = PTHREAD_MUTEX_INITIALIZER;
m_old_buf = stream.rdbuf(); m_old_buf = stream.rdbuf();
stream.rdbuf(this); stream.rdbuf(this);
} }
@ -55,8 +57,11 @@ public:
~qDebugStream(){ ~qDebugStream(){
// output anything that is left // output anything that is left
if (!m_string.empty()) if (!m_string.empty()) {
pthread_mutex_lock(&mutex);
QApplication::postEvent(log_window, new qStreamEvent(m_string.c_str())); QApplication::postEvent(log_window, new qStreamEvent(m_string.c_str()));
pthread_mutex_unlock(&mutex);
}
m_stream.rdbuf(m_old_buf); m_stream.rdbuf(m_old_buf);
} }
@ -65,8 +70,10 @@ public:
protected: protected:
virtual int_type overflow(int_type v){ virtual int_type overflow(int_type v){
if (v == '\n'){ if (v == '\n'){
pthread_mutex_lock(&mutex);
QApplication::postEvent(log_window, new qStreamEvent(m_string.c_str())); QApplication::postEvent(log_window, new qStreamEvent(m_string.c_str()));
m_string.erase(m_string.begin(), m_string.end()); m_string.erase(m_string.begin(), m_string.end());
pthread_mutex_unlock(&mutex);
} }
else else
m_string += v; m_string += v;
@ -76,7 +83,9 @@ protected:
//------------------------------------------------------------------------------------------------------------------------------------------------- //-------------------------------------------------------------------------------------------------------------------------------------------------
virtual streamsize xsputn(const char *p, streamsize n) { virtual streamsize xsputn(const char *p, streamsize n) {
pthread_mutex_lock(&mutex);
m_string.append(p, p + n); m_string.append(p, p + n);
//changed from uint because of 64 bit //changed from uint because of 64 bit
int pos = 0; int pos = 0;
@ -88,12 +97,14 @@ protected:
m_string.erase(m_string.begin(), m_string.begin() + pos + 1); m_string.erase(m_string.begin(), m_string.begin() + pos + 1);
} }
} }
pthread_mutex_unlock(&mutex);
return n; return n;
} }
//------------------------------------------------------------------------------------------------------------------------------------------------- //-------------------------------------------------------------------------------------------------------------------------------------------------
private: private:
pthread_mutex_t mutex;
ostream &m_stream; ostream &m_stream;
streambuf *m_old_buf; streambuf *m_old_buf;
string m_string; string m_string;

View File

@ -153,14 +153,14 @@ void qTabAdvanced::SetupWidgetWindow(){
spinControlPort->setValue(det->getControlPort()); spinControlPort->setValue(det->getControlPort());
spinStopPort->setValue(det->getStopPort()); spinStopPort->setValue(det->getStopPort());
spinTCPPort->setValue(det->getReceiverPort()); spinTCPPort->setValue(det->getReceiverPort());
spinUDPPort->setValue(atoi(det->getReceiverUDPPort())); spinUDPPort->setValue(atoi(det->getReceiverUDPPort().c_str()));
cout << "Getting network information" << endl; cout << "Getting network information" << endl;
dispIP->setText(det->getDetectorIP()); dispIP->setText(det->getDetectorIP().c_str());
dispMAC->setText(det->getDetectorMAC()); dispMAC->setText(det->getDetectorMAC().c_str());
dispRxrHostname->setText(det->getReceiver()); dispRxrHostname->setText(det->getReceiver().c_str());
dispUDPIP->setText(det->getReceiverUDPIP()); dispUDPIP->setText(det->getReceiverUDPIP().c_str());
dispUDPMAC->setText(det->getReceiverUDPMAC()); dispUDPMAC->setText(det->getReceiverUDPMAC().c_str());
//check if its online and set it to red if offline //check if its online and set it to red if offline
@ -785,10 +785,10 @@ void qTabAdvanced::SetNetworkParameters(){
disconnect(dispUDPIP, SIGNAL(editingFinished()), this, SLOT(SetNetworkParameters())); disconnect(dispUDPIP, SIGNAL(editingFinished()), this, SLOT(SetNetworkParameters()));
disconnect(dispUDPMAC, SIGNAL(editingFinished()), this, SLOT(SetNetworkParameters())); disconnect(dispUDPMAC, SIGNAL(editingFinished()), this, SLOT(SetNetworkParameters()));
dispIP->setText(QString(det->setDetectorIP(dispIP->text().toAscii().constData()))); dispIP->setText(QString(det->setDetectorIP(dispIP->text().toAscii().constData()).c_str()));
dispMAC->setText(QString(det->setDetectorMAC(dispMAC->text().toAscii().constData()))); dispMAC->setText(QString(det->setDetectorMAC(dispMAC->text().toAscii().constData()).c_str()));
dispUDPIP->setText(QString(det->setReceiverUDPIP(dispUDPIP->text().toAscii().constData()))); dispUDPIP->setText(QString(det->setReceiverUDPIP(dispUDPIP->text().toAscii().constData()).c_str()));
dispUDPMAC->setText(QString(det->setReceiverUDPMAC(dispUDPMAC->text().toAscii().constData()))); dispUDPMAC->setText(QString(det->setReceiverUDPMAC(dispUDPMAC->text().toAscii().constData()).c_str()));
qDefs::checkErrorMessage(det,"qTabAdvanced::SetNetworkParameters"); qDefs::checkErrorMessage(det,"qTabAdvanced::SetNetworkParameters");
connect(dispIP, SIGNAL(editingFinished()), this, SLOT(SetNetworkParameters())); connect(dispIP, SIGNAL(editingFinished()), this, SLOT(SetNetworkParameters()));
@ -806,7 +806,7 @@ void qTabAdvanced::SetReceiver(){
cout << "Setting Receiver" << endl; cout << "Setting Receiver" << endl;
#endif #endif
string outdir = myDet->getFilePath(); string outdir = myDet->getFilePath();
dispRxrHostname->setText(QString(det->setReceiver(dispRxrHostname->text().toAscii().constData()))); dispRxrHostname->setText(QString(det->setReceiver(dispRxrHostname->text().toAscii().constData()).c_str()));
qDefs::checkErrorMessage(det,"qTabAdvanced::SetReceiver"); qDefs::checkErrorMessage(det,"qTabAdvanced::SetReceiver");
det->setFilePath(outdir); det->setFilePath(outdir);
qDefs::checkErrorMessage(det,"qTabAdvanced::SetReceiver"); qDefs::checkErrorMessage(det,"qTabAdvanced::SetReceiver");
@ -1059,13 +1059,13 @@ void qTabAdvanced::SetDetector(int index){
spinControlPort->setValue(det->getControlPort()); spinControlPort->setValue(det->getControlPort());
spinStopPort->setValue(det->getStopPort()); spinStopPort->setValue(det->getStopPort());
spinTCPPort->setValue(det->getReceiverPort()); spinTCPPort->setValue(det->getReceiverPort());
spinUDPPort->setValue(atoi(det->getReceiverUDPPort())); spinUDPPort->setValue(atoi(det->getReceiverUDPPort().c_str()));
dispIP->setText(det->getDetectorIP()); dispIP->setText(det->getDetectorIP().c_str());
dispMAC->setText(det->getDetectorMAC()); dispMAC->setText(det->getDetectorMAC().c_str());
dispRxrHostname->setText(det->getReceiver()); dispRxrHostname->setText(det->getReceiver().c_str());
dispUDPIP->setText(det->getReceiverUDPIP()); dispUDPIP->setText(det->getReceiverUDPIP().c_str());
dispUDPMAC->setText(det->getReceiverUDPMAC()); dispUDPMAC->setText(det->getReceiverUDPMAC().c_str());
//check if its online and set it to red if offline //check if its online and set it to red if offline
@ -1262,20 +1262,20 @@ void qTabAdvanced::Refresh(){
disconnect(dispUDPMAC, SIGNAL(editingFinished()), this, SLOT(SetNetworkParameters())); disconnect(dispUDPMAC, SIGNAL(editingFinished()), this, SLOT(SetNetworkParameters()));
disconnect(btnRxr, SIGNAL(clicked()), this, SLOT(SetReceiver())); disconnect(btnRxr, SIGNAL(clicked()), this, SLOT(SetReceiver()));
dispIP->setText(det->getDetectorIP()); dispIP->setText(det->getDetectorIP().c_str());
dispMAC->setText(det->getDetectorMAC()); dispMAC->setText(det->getDetectorMAC().c_str());
//so that updated status //so that updated status
if(det->setReceiverOnline()==slsDetectorDefs::ONLINE_FLAG) if(det->setReceiverOnline()==slsDetectorDefs::ONLINE_FLAG)
det->checkReceiverOnline(); det->checkReceiverOnline();
comboRxrOnline->setCurrentIndex(det->setReceiverOnline()); comboRxrOnline->setCurrentIndex(det->setReceiverOnline());
dispRxrHostname->setText(det->getReceiver()); dispRxrHostname->setText(det->getReceiver().c_str());
spinTCPPort->setValue(det->getReceiverPort()); spinTCPPort->setValue(det->getReceiverPort());
spinUDPPort->setValue(atoi(det->getReceiverUDPPort())); spinUDPPort->setValue(atoi(det->getReceiverUDPPort().c_str()));
dispUDPIP->setText(det->getReceiverUDPIP()); dispUDPIP->setText(det->getReceiverUDPIP().c_str());
dispUDPMAC->setText(det->getReceiverUDPMAC()); dispUDPMAC->setText(det->getReceiverUDPMAC().c_str());
//connect //connect
connect(spinTCPPort, SIGNAL(valueChanged(int)), this, SLOT(SetRxrTCPPort(int))); connect(spinTCPPort, SIGNAL(valueChanged(int)), this, SLOT(SetRxrTCPPort(int)));