From 2ec1e08081fefb08047042db7489e437007b6711 Mon Sep 17 00:00:00 2001 From: Dhanya Maliakal Date: Mon, 19 Jun 2017 14:19:30 +0200 Subject: [PATCH] changed API for setting network parameter to remove memory leak, fixed lock issues in streaming print outs to gui --- slsDetectorGui/include/qDebugStream.h | 13 +++++++- slsDetectorGui/src/qTabAdvanced.cpp | 46 +++++++++++++-------------- slsDetectorGui/src/qTabMessages.cpp | 2 +- 3 files changed, 36 insertions(+), 25 deletions(-) diff --git a/slsDetectorGui/include/qDebugStream.h b/slsDetectorGui/include/qDebugStream.h index 816fcd78d..570e3c260 100644 --- a/slsDetectorGui/include/qDebugStream.h +++ b/slsDetectorGui/include/qDebugStream.h @@ -17,6 +17,7 @@ #include #include + #include #include #include @@ -47,6 +48,7 @@ class qDebugStream : public basic_streambuf { public: qDebugStream(ostream &stream, QWidget* w) : m_stream(stream), log_window(w) { + mutex = PTHREAD_MUTEX_INITIALIZER; m_old_buf = stream.rdbuf(); stream.rdbuf(this); } @@ -55,8 +57,11 @@ public: ~qDebugStream(){ // 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())); + pthread_mutex_unlock(&mutex); + } m_stream.rdbuf(m_old_buf); } @@ -65,8 +70,10 @@ public: protected: virtual int_type overflow(int_type v){ if (v == '\n'){ + pthread_mutex_lock(&mutex); QApplication::postEvent(log_window, new qStreamEvent(m_string.c_str())); m_string.erase(m_string.begin(), m_string.end()); + pthread_mutex_unlock(&mutex); } else m_string += v; @@ -76,7 +83,9 @@ protected: //------------------------------------------------------------------------------------------------------------------------------------------------- virtual streamsize xsputn(const char *p, streamsize n) { + pthread_mutex_lock(&mutex); m_string.append(p, p + n); + //changed from uint because of 64 bit int pos = 0; @@ -88,12 +97,14 @@ protected: m_string.erase(m_string.begin(), m_string.begin() + pos + 1); } } + pthread_mutex_unlock(&mutex); return n; } //------------------------------------------------------------------------------------------------------------------------------------------------- private: + pthread_mutex_t mutex; ostream &m_stream; streambuf *m_old_buf; string m_string; diff --git a/slsDetectorGui/src/qTabAdvanced.cpp b/slsDetectorGui/src/qTabAdvanced.cpp index 0d0e13cf2..c7395e801 100644 --- a/slsDetectorGui/src/qTabAdvanced.cpp +++ b/slsDetectorGui/src/qTabAdvanced.cpp @@ -153,14 +153,14 @@ void qTabAdvanced::SetupWidgetWindow(){ spinControlPort->setValue(det->getControlPort()); spinStopPort->setValue(det->getStopPort()); spinTCPPort->setValue(det->getReceiverPort()); - spinUDPPort->setValue(atoi(det->getReceiverUDPPort())); + spinUDPPort->setValue(atoi(det->getReceiverUDPPort().c_str())); cout << "Getting network information" << endl; - dispIP->setText(det->getDetectorIP()); - dispMAC->setText(det->getDetectorMAC()); - dispRxrHostname->setText(det->getReceiver()); - dispUDPIP->setText(det->getReceiverUDPIP()); - dispUDPMAC->setText(det->getReceiverUDPMAC()); + dispIP->setText(det->getDetectorIP().c_str()); + dispMAC->setText(det->getDetectorMAC().c_str()); + dispRxrHostname->setText(det->getReceiver().c_str()); + dispUDPIP->setText(det->getReceiverUDPIP().c_str()); + dispUDPMAC->setText(det->getReceiverUDPMAC().c_str()); //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(dispUDPMAC, SIGNAL(editingFinished()), this, SLOT(SetNetworkParameters())); - dispIP->setText(QString(det->setDetectorIP(dispIP->text().toAscii().constData()))); - dispMAC->setText(QString(det->setDetectorMAC(dispMAC->text().toAscii().constData()))); - dispUDPIP->setText(QString(det->setReceiverUDPIP(dispUDPIP->text().toAscii().constData()))); - dispUDPMAC->setText(QString(det->setReceiverUDPMAC(dispUDPMAC->text().toAscii().constData()))); + dispIP->setText(QString(det->setDetectorIP(dispIP->text().toAscii().constData()).c_str())); + dispMAC->setText(QString(det->setDetectorMAC(dispMAC->text().toAscii().constData()).c_str())); + dispUDPIP->setText(QString(det->setReceiverUDPIP(dispUDPIP->text().toAscii().constData()).c_str())); + dispUDPMAC->setText(QString(det->setReceiverUDPMAC(dispUDPMAC->text().toAscii().constData()).c_str())); qDefs::checkErrorMessage(det,"qTabAdvanced::SetNetworkParameters"); connect(dispIP, SIGNAL(editingFinished()), this, SLOT(SetNetworkParameters())); @@ -806,7 +806,7 @@ void qTabAdvanced::SetReceiver(){ cout << "Setting Receiver" << endl; #endif 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"); det->setFilePath(outdir); qDefs::checkErrorMessage(det,"qTabAdvanced::SetReceiver"); @@ -1059,13 +1059,13 @@ void qTabAdvanced::SetDetector(int index){ spinControlPort->setValue(det->getControlPort()); spinStopPort->setValue(det->getStopPort()); spinTCPPort->setValue(det->getReceiverPort()); - spinUDPPort->setValue(atoi(det->getReceiverUDPPort())); + spinUDPPort->setValue(atoi(det->getReceiverUDPPort().c_str())); - dispIP->setText(det->getDetectorIP()); - dispMAC->setText(det->getDetectorMAC()); - dispRxrHostname->setText(det->getReceiver()); - dispUDPIP->setText(det->getReceiverUDPIP()); - dispUDPMAC->setText(det->getReceiverUDPMAC()); + dispIP->setText(det->getDetectorIP().c_str()); + dispMAC->setText(det->getDetectorMAC().c_str()); + dispRxrHostname->setText(det->getReceiver().c_str()); + dispUDPIP->setText(det->getReceiverUDPIP().c_str()); + dispUDPMAC->setText(det->getReceiverUDPMAC().c_str()); //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(btnRxr, SIGNAL(clicked()), this, SLOT(SetReceiver())); - dispIP->setText(det->getDetectorIP()); - dispMAC->setText(det->getDetectorMAC()); + dispIP->setText(det->getDetectorIP().c_str()); + dispMAC->setText(det->getDetectorMAC().c_str()); //so that updated status if(det->setReceiverOnline()==slsDetectorDefs::ONLINE_FLAG) det->checkReceiverOnline(); comboRxrOnline->setCurrentIndex(det->setReceiverOnline()); - dispRxrHostname->setText(det->getReceiver()); + dispRxrHostname->setText(det->getReceiver().c_str()); spinTCPPort->setValue(det->getReceiverPort()); - spinUDPPort->setValue(atoi(det->getReceiverUDPPort())); + spinUDPPort->setValue(atoi(det->getReceiverUDPPort().c_str())); - dispUDPIP->setText(det->getReceiverUDPIP()); - dispUDPMAC->setText(det->getReceiverUDPMAC()); + dispUDPIP->setText(det->getReceiverUDPIP().c_str()); + dispUDPMAC->setText(det->getReceiverUDPMAC().c_str()); //connect connect(spinTCPPort, SIGNAL(valueChanged(int)), this, SLOT(SetRxrTCPPort(int))); diff --git a/slsDetectorGui/src/qTabMessages.cpp b/slsDetectorGui/src/qTabMessages.cpp index 4c9a65850..460351164 100644 --- a/slsDetectorGui/src/qTabMessages.cpp +++ b/slsDetectorGui/src/qTabMessages.cpp @@ -81,7 +81,7 @@ void qTabMessages::Initialization(){ //------------------------------------------------------------------------------------------------------------------------------------------------- void qTabMessages::customEvent(QEvent *e) { - if (e->type() == (STREAMEVENT)){ + if (e->type() == (STREAMEVENT)){ QString temp = ((qStreamEvent*)e)->getString(); dispLog->append(temp); }