Memory leak in QSerialBus work - c++

Hello everyone!
I've got problem with my ModBus realisation via QSerialBus library on Qt 5.8.
The problem is - when I try to read a list of discrete inputs on a high speed, the program catch a memory leak - about 300-350Kb per second.
So, here the code of function that causes leak:
void ModBus::queue()
{
if(!_readList.isEmpty())
{
if(!checkState())
{
_readList.clear();
_curPos = DiscInputPos::Full;
queue();
return;
}
QModbusDataUnit readData = QModbusDataUnit(QModbusDataUnit::DiscreteInputs, _readList.first(), 1);
if(auto *reply = _client->sendReadRequest(readData, _serverNum))
{
if(!reply->isFinished())
{
connect(reply, &QModbusReply::errorOccurred, [=] (QModbusDevice::Error e)
{
reply->deleteLater();
if(e == QModbusDevice::TimeoutError)
{
_readList.clear();
_curPos = DiscInputPos::Full;
stop();
start();
queue();
}
});
connect(reply, &QModbusReply::finished, this, [=]
{
if(reply->error() == QModbusDevice::NoError)
{
if(reply->result().valueCount() > 0)
{
_curPos = (DiscInputPos) reply->result().value(0);
if(_curPos == DiscInputPos::Clear)
_readList.removeFirst();
else
_readList.clear();
}
}
else
{
_readList.clear();
_curPos = DiscInputPos::Full;
}
reply->deleteLater();
queue();
});
}
else
delete reply;
}
else
{
delete reply;
_readList.clear();
_curPos = DiscInputPos::Full;
queue();
}
}
else
{
emit sendReadResult(_curPos);
_curPos = DiscInputPos::Full;
}
}
_client — QModbusTcpClient
bool checkState() — does nothing special unless checking Connected state.
Maximum _readList size is 4.
If I comment a part of code with _client->sendReadRequest - everything goes alright without any leak. Can anybody explain - what it can be?
Thanks in advance!

I solved this problem via timer on 0.5 sec (less causes leaks too), that ticks as single shot from class that check the answer from ModBus class. There is still leaks, but much-much less. If 1 sec - there is no leaks.
connect(d, &ModBus::sendReadResult, this, &StatusesWorker::getRead);
void StatusesWorker::getRead(DiscInputPos s)
{
bool cleared = (s == DiscInputPos::Clear);
if(!cleared)
emit badSensors();
else
emit clearSensors();
QTimer::singleShot(500, [=] { checkSensor(id); });
}
}
But I think this is bad solution.
UPD.
Qt Community solved the problem like that:
connect(reply, &QModbusReply::destroyed, [this] { QTimer::singleShot(60, [this] { queue(); }); });
And it's work faster without leaks. Of course, checkSensor() function now without timer.

Related

Can't get an item from tableWidget in QT

I have the function like this below, and global QVector<pid_t> pid; in the header file which elements are Linux process ids. But when I'm trying to push the button "priority" - programm unexpectedly finishes. Due to qDebugs I've realized that function interrupts after if statement. And I can not understand the matter of this problem. Function:
void MainWindow::on_priority_clicked()
{
int curI = ui->tableWidget->currentRow();
int prio = ui->prioritySpinBox->value();
try{
if(ui->tableWidget->item(curI,1)->text().isNull())
throw curI;
else {
setpriority(PRIO_PROCESS, pid.at(curI),prio);
QLabel *labelPrio = new QLabel(ui->tableWidget);
labelPrio->setText(QString::number(getpriority(PRIO_PROCESS, pid.at(curI))));
ui->tableWidget->setCellWidget(curI, 3, labelPrio);
}
}
catch(int x)
{
QMessageBox::warning(this, "Error", "Process " + QString::number(x+1) + " is not created");
}
}
Not sure if this is your problem, but if ui->tableWidget->item(curI,1) doesn't exist (or is null), then calling ->text() on it will cause a crash.
You might need to check if it exists first:
void MainWindow::on_priority_clicked()
{
int curI = ui->tableWidget->currentRow();
int prio = ui->prioritySpinBox->value();
try{
if(ui->tableWidget->item(curI,1) != nullptr)
....

Qt Multiple Async While Loops and qApp->processEvents();

I am trying to do a project is creating some graphics on window with Qt GUI C++ 5.6.2.
I have two methods named 'createVerticalSpeedIndicator' and 'createAirSpeedIndicator'. These methods need to create some graphics with a while(1) loop and use qApp->processEvents(); on window and they are doing it perfectly when one of them is working the other one is deactive. But I need to run both of them simultanously and always.
What can I do to run them simultanously and always.
Thank you So much
The solution is to invert the control flow. The while() { ... processEvents() ... } is an anti-pattern in asynchronous code, because it assumes that you have locus of control whereas you really don't. You're lucky that you didn't run out of stack since processEvents could potentially re-enter the createXxx methods.
Here's a complete example of a transformation:
// Input
void Class::createVerticalSpeedIndicator() {
for (int i = 0; i < 100; i ++) {
doStep(i);
QCoreApplication::processEvents();
}
}
// Step 1 - factor out state
void Class::createVerticalSpeedIndicator() {
int i = 0;
while (i < 100) {
doStep(i);
QCoreApplication::processEvents();
i++;
}
};
// Step 2 - convert to continuation form
void Class::createVerticalSpeedIndicator() {
int i = 0;
auto continuation = [=]() mutable {
if (!(i < 100))
return false;
doStep(i);
QCoreApplication::processEvents();
i++;
return true;
};
while (continuation());
};
// Step 3 - execute the continuation asynchronously
auto Class::createVerticalSpeedIndicator() {
int i = 0;
return async(this, [=]() mutable {
if (!(i < 100))
return false;
doStep(i);
i++; // note the removal of processEvents here
return true;
});
};
template <typename F> void async(QObject * parent, F && continuation) {
auto timer = new QTimer(parent);
timer->start(0);
connect(timer, &QTimer::timeout, [timer, c = std::move(continuation)]{
if (!c())
timer->deleteLater();
});
}
At that point you can apply the same transformation to createAirSpeedIndicator and start them in the constructor of your class:
Class::Class(QWidget * parent) : QWidget(parent) {
...
createVerticalSpeedIndicator();
createAirSpeedIndicator();
}
Both tasks will run asynchronously and pseudo-concurrently within the main thread, i.e. each task will alternatively execute a single step.
Suppose we wanted to chain the tasks, i.e. have a task start only after the previous one has finished. The modification in the user code can be simple:
Class::Class(QWidget * parent) : QWidget(parent) {
...
createVerticalSpeedIndicator()
>> createAirSpeedIndicator()
>> someOtherTask();
}
The async function must now return a class that allows such connections to be made:
struct TaskTimer {
QTimer * timer;
TaskTimer & operator>>(const TaskTimer & next) {
next.timer->stop();
connect(timer, &QObject::destroyed, next.timer, [timer = next.timer]{
timer->start(0);
});
timer = next.timer;
return *this;
}
};
template <typename F> TaskTimer async(QObject * parent, F && continuation) {
TaskTimer task{new QTimer(parent)};
task.timer->start(0);
connect(task.timer, &QTimer::timeout,
[timer = task.timer, c = std::move(continuation)]{
if (!c())
timer->deleteLater();
});
return task;
}

Abnormal output when reading from serial port in QT

I am using QT and QCustomPlot to create a real time plotting tool, and the data of the plot is read from the Arduino UNO board. My application succeeded in plotting while the data is a total mess. Here is my code below (Some code is from QCustomPlot website):
void Dialog::realtimeDataSlot()
{
bool currentPortNameChanged = false;
QString currentPortName;
if (currentPortName != portName) {
currentPortName = portName;
currentPortNameChanged = true;
}
QString currentRequest = request;
QSerialPort serial;
if (currentPortNameChanged) {
serial.close();
serial.setPortName(currentPortName);
if (!serial.open(QIODevice::ReadOnly)) {
return;
}
}
static QTime time(QTime::currentTime());
// calculate two new data points:
double key = time.elapsed()/1000.0;
static double lastPointKey = 0;
if (key-lastPointKey > 0.002) // at most add point every 2 ms
{
// add data to lines:
if(serial.waitForReadyRead(-1)){
data = serial.readAll();
QTextStream(stdout) << "HERE:" << data.toDouble() << endl;
customPlot->graph(0)->addData(key, data.toDouble());
customPlot->graph(0)->rescaleValueAxis(); //rescale value (vertical) axis to fit the current data:
lastPointKey = key;
customPlot->xAxis->setRange(key, 8, Qt::AlignRight);
customPlot->replot();
static double lastFpsKey;
static int frameCount;
++frameCount;
if (key-lastFpsKey > 2) // average fps over 2 seconds
{
lastFpsKey = key;
frameCount = 0;
}
}
}
// calculate frames per second:
if (currentPortName != portName) {
currentPortName = portName;
currentPortNameChanged = true;
} else {
currentPortNameChanged = false;
}
}
When I Tried to print out the data I read from the serial port, I found the following:
HERE:1
HERE:15
HERE:150
HERE:149
HERE:149
HERE:149
HERE:150
HERE:150
HERE:15
HERE:150
HERE:149
HERE:49
HERE:150
HERE:150
HERE:1
HERE:150
The values around 150 are normal while the value that are 0, 1 to others are not. Also it is not print out at a stable speed. I don't know what happened to this, and thanks to whoever may help, and I would appreciate it if there is any better ways to implement this.
The problem here is that it is not guaranteed that the serial transmission is received all at once. So it is better to let the serial to be processed somewhere else, for instance:
// in the class definition
QSerialPort serialPort;
private slots:
void handleReadyRead();
private:
QByteArray serialBuffer;
volatile double lastSerialValue;
// In the initialization part (not the realtimeDataSlot function)
lastSerialValue = qQNaN();
serialPort.setPortName(currentPortName);
connect(&serialPort, &QSerialPort::readyRead, this, &Dialog::handleReadyRead, Qt::UniqueConnection);
if (!serialPort.open(QIODevice::ReadOnly)) {
return;
}
serialBuffer.clear();
// Other functions:
void Dialog::realtimeDataSlot()
{
...
if (key-lastPointKey > 0.002) // at most add point every 2 ms
{
if (!qIsNaN(lastSerialData))
{
// use lastSerialValue as the data.toDouble() you had before, then, at the end
lastSerialValue = qQNaN();
}
...
}
void Dialog::handleReadyRead()
{
serialBuffer.append(serialPort.readAll());
int serPos;
while ((serPos = serialBuffer.indexOf('\n')) >= 0)
{
bool ok;
double tempValue = QString::fromLatin1(serialBuffer.left(serPos)).toDouble(&ok);
if (ok) lastSerialValue = tempValue;
serialBuffer = serialBuffer.mid(serPos+1);
}
}
Explanation: whenever you receive something from the arduino the bytes are appended to a buffer. Then the byte array is parsed looking for a terminator, and if found the byte array is split and analysed. When the other function needs the data, it simply pulls the most recent one saved in the variable.
NOTE 1: I saw that you used a binary transmission. The problem is that you do not have any way to determine where the data begins and end in this way. For instance, if you receive 0x01 0x02 0x03 0x04 and you know that there are 3 bytes, are they 01..03 or 02..04 or 03, 04 and a missing one or...? The version I implemented requires you to send data in string format with a new-line terminator (simplest version, you just have to write Serial.println(doubleValue); in the arduino code), but if you need the binary version I can give you some hints
NOTE 2: The code I wrote is NOT thread safe. It will work only if the realtimeDataSlot and the handleReadyRead are called in the same thread. Note that if they belong to the same object and are called through signals this is guaranteed.
Now, this should work. But I highly discourage you from doing this. I don't know who needs to call the realtimeDataSlot(), but I think that the most correct version is something like this:
// in the class definition
QSerialPort serialPort;
private slots:
void handleReadyRead();
void receivedData(double val);
private:
QByteArray serialBuffer;
signals:
void newData(double data);
// In the initialization part (not the realtimeDataSlot function)
serialPort.setPortName(currentPortName);
connect(&serialPort, &QSerialPort::readyRead, this, &Dialog::handleReadyRead, Qt::UniqueConnection);
connect(this, &Dialog::newData, this, &Dialog::receivedData, Qt::UniqueConnection);
if (!serialPort.open(QIODevice::ReadOnly)) {
return;
}
serialBuffer.clear();
// Other functions:
void Dialog::receivedData(double val)
{
double key = time.elapsed()/1000.0;
static double lastPointKey = 0;
if (key-lastPointKey > 0.002) // at most add point every 2 ms
{
QTextStream(stdout) << "HERE:" << data.toDouble() << endl;
customPlot->graph(0)->addData(key, data.toDouble());
customPlot->graph(0)->rescaleValueAxis();
...
}
}
void Dialog::handleReadyRead()
{
serialBuffer.append(serialPort.readAll());
int serPos;
while ((serPos = serialBuffer.indexOf('\n')) >= 0)
{
bool ok;
double tempValue = QString::fromLatin1(serialBuffer.left(serPos)).toDouble(&ok);
if (ok) emit newData(tempValue);
serialBuffer = serialBuffer.mid(serPos+1);
}
}
So keep the graph responsive to events (received a new data) instead of to a timer.
One more thing: I removed the port change on purpose. I suggest you to handle it in another way: put a button to start and stop the serial, and when the serial port is started prevent the user from changing the port name. This way the user will explicitely need to shut it down when he needs to change the port. If you want your version, however, don't include it in your code, but make a slot on its own to call whenever you need to change the port name:
void changeSerialPortName(QString newName)
{
if (newName != serialPort.portName()) {
if (serialPort.isOpen())
serialPort.close();
serialPort.setPortName(newName);
if (!serialPort.open(QIODevice::ReadOnly)) {
return;
}
}
}

Best way to design multple and nested GET/POST in QT with QNetworkManager

the doubt i have is about the correct design of a software which implements multiple and nested GET/POST request.
Say you have to run a login() function which requires a GET and a POST and then retrieveXYZ() which requires two GETs (and so on, scalable).
The way i was thinkig to do it was like
mainwindow.cpp
//code
login();
retrieveXYZ();
//code
Mainwindow::login(){
//code
connect(nam, SIGNAL(finished()), this, SLOT(onGetLoginFinished()));
nam->get(...);
}
Mainwindow::onGetLoginFinished(){
//do stuff
connect(nam, SIGNAL(finished()), this, SLOT(onPostLoginFinished()));
nam->post(...);
}
Mainwindow::onPostLoginFinished(){
//do stuff
}
Mainwindow::retrieveXYZ(){
//code
connect(nam, SIGNAL(finished()), this, SLOT(onGet1RetrieveFinished()));
nam->get();
//code
}
Mainwindow::onGet1RetrieveXYZFinished(){
//do stuff
connect(nam, SIGNAL(finished()), this, SLOT(onGet2RetrieveFinished()));
nam->get();
}
or should i use something like QSignalMapper ?
Which are the most correct/efficient way to do so? i've seen people using sender() cast but i didn't understand the point.
Basically i would like to retrieve the particular reply finished() signal rather than the general one (or of the qnam)
This method may work but it is not nice and clean to me
Is this the best we can get?
http://www.johanpaul.com/blog/2011/07/why-qnetworkaccessmanager-should-not-have-the-finishedqnetworkreply-signal/
Moving the connect approach to the reply?
I got something like:
struct RequestResult {
int httpCode;
QByteArray content;
};
RequestResult
ExecuteRequest(const std::function<QNetworkReply*(QNetworkAccessManager&)>& action,
const std::chrono::milliseconds& timeOut)
{
QEventLoop eLoop;
QTimer timeOutTimer;
QNetworkAccessManager nam;
QObject::connect(&timeOutTimer, &QTimer::timeout, &eLoop, &QEventLoop::quit);
QObject::connect(&nam, &QNetworkAccessManager::finished, &eLoop, &QEventLoop::quit);
timeOutTimer.setSingleShot(true);
timeOutTimer.setInterval(timeOut.count());
timeOutTimer.start();
auto resetTimeOut = [&timeOutTimer]() { timeOutTimer.start(); };
QNetworkReply* reply = action(nam);
QObject::connect(reply, &QNetworkReply::uploadProgress, resetTimeOut);
QObject::connect(reply, &QNetworkReply::downloadProgress, resetTimeOut);
eLoop.exec();
if (!timeOutTimer.isActive())
{
throw std::runtime_error("Time out"); // Probably custom exception
}
const int httpStatus
= reply->attribute(QNetworkRequest::Attribute::HttpStatusCodeAttribute).toInt();
auto content = TakeContent(*reply); // reply->readAll and decompression
return RequestResult{httpStatus, content};
}
And then functions for get/delete/post/.. which are similar to
auto RequestGet(const QNetworkRequest& request) {
return ExecuteRequest([&](QNetworkAccessManager& nam) { return nam.get(request); },
timeOut);
}
auto RequestDelete(const QNetworkRequest& request) {
return ExecuteRequest([&](QNetworkAccessManager& nam) {
return nam.deleteResource(request);
},
timeOut);
}
auto RequestPost(const QNetworkRequest& request, QHttpMultiPart& multiPart)
{
return ExecuteRequest([&](QNetworkAccessManager& nam) {
return nam.post(request, &multiPart);
},
timeOut);
}
Then, for your code, I would do something like
Mainwindow::login()
{
const auto getRes = RequestGet(..);
// ...
const auto postRes = RequestPost(..);
// ...
}
And you may use thread and future if you want not blocking calls.

Memory leak with post requests and QNetworkAccessManager

I'm making a program that uses lots of timer and, at intervals of 4 seconds, does an online post to a php script.
I'm coding in QtCreator 5.1. I use classes just like the ones below.
The one below just populates a task list, but throughout the course of 8 to 12 hours, the memory that the program takes up just keep rising and rising gradually.
What am I doing wrong while using this class?
I have to be able to keep posting online like I already am, about every 4 to 8 seconds.
Here's a simple class for taking care of one of my processes:
Header file: tasklistprocess.h
#ifndef TASKLISTPROCESS_H
#define TASKLISTPROCESS_H
#include <QThread>
#include <QtCore>
#include <QNetworkRequest>
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QListWidget>
#include <QTabWidget>
#include "globalhelper.h"
#include "securityinfo.h"
class TaskListProcess : public QThread
{
Q_OBJECT
public:
explicit TaskListProcess(QListWidget *obj_main, QTabWidget *tabs_main, QString user, QObject *parent = 0);
signals:
void upTaskStorage(int key,QHash<QString,QString> item);
private:
GlobalHelper gh;
Securityinfo sci;
QNetworkAccessManager *nam;
QNetworkRequest request;
QByteArray data;
// this is the disposable params for reusage through out the class
QUrlQuery params;
QString post_data;
QString user_name;
QTimer *tasklist_tmr;
bool get_task_list;
QListWidget *obj;
QTabWidget *tabs;
private slots:
void setTaskList();
void replyFinished(QNetworkReply *reply);
void sendPost(QString file_name, QUrlQuery params);
};
#endif // TASKLISTPROCESS_H`
Source file: tasklistprocess.cpp
#include "tasklistprocess.h"
TaskListProcess::TaskListProcess(QListWidget *obj_main, QTabWidget *tabs_main, QString user, QObject *parent) :
QThread(parent)
{
user_name = user;
get_task_list = false;
obj = obj_main;
tabs = tabs_main;
tasklist_tmr = new QTimer(this);
connect(this,SIGNAL(started()),this,SLOT(setTaskList()));
connect(tasklist_tmr,SIGNAL(timeout()),this,SLOT(setTaskList()));
nam = new QNetworkAccessManager(this);
request.setHeader(QNetworkRequest::ContentTypeHeader,"application/x-www-form-urlencoded");
request.setRawHeader( "User-Agent" , "Mozilla Firefox" );
// here we connect up the data stream and data reply signals
connect(nam, SIGNAL(finished(QNetworkReply*)), this, SLOT(replyFinished(QNetworkReply*)));
}
void TaskListProcess::setTaskList()
{
qDebug() << "Your task list was set";
bool in = false;
if(!(tasklist_tmr->isActive()))
{
tasklist_tmr->start(10000);
in = true;
}
if(!(get_task_list))
{
params.clear();
params.addQueryItem("user_name", user_name);
params.addQueryItem("logged_in", "1");
sendPost("getTaskList.php",params);
get_task_list = true;
}
else
{
if(post_data.contains("|*|"))
{
//here i retrieve a piece of information from a php script which is stored in a custom string format
// here we clear the list for the new data to be put in
if(obj->count()>0)
{
obj->clear();
}
int key = 0;
foreach(QString inner_task,post_data.split("|*|"))
{
QHash<QString,QString> task_cont;
//qDebug() << " ";
if(inner_task.contains("*,*"))
{
foreach(QString task_val,inner_task.split("*,*"))
{
if(task_val.contains("*=*"))
{
QStringList key_pairs = task_val.split("*=*");
task_cont.insert(key_pairs[0],key_pairs[1]);
if(key_pairs[0] == "tt")
{
QString val_in;
if(key_pairs[1].length()>10)
{
// this sets the title to the shortened version
// if the string length is too long
val_in = key_pairs[1].left(10) + "....";
}
else
{
val_in = key_pairs[1];
}
obj->addItem("Task :" + QString::fromUtf8(key_pairs[1].toStdString().c_str()));
}
}
}
}
//task_storage.insert(key,task_cont);
emit upTaskStorage(key,task_cont);
key ++;
}
}
get_task_list = false;
}
// here we're checking to see if they are looking at the task tab so it doesn't keep changing
// back and forth between the tabs
bool change = true;
if(tabs->currentIndex() != 0)
{
change = false;
}
if(change)
{
tabs->setCurrentIndex(0);
}else if(in)
{
tabs->setCurrentIndex(0);
}
}
void TaskListProcess::replyFinished(QNetworkReply *reply)
{
if (reply->error() != QNetworkReply::NoError) {
qDebug() << "Error in" << reply->url() << ":" << reply->errorString();
return;
}
QString data = reply->readAll().trimmed();
post_data = data;
if(get_task_list)
{
setTaskList();
}
}
void TaskListProcess::sendPost(QString file_name, QUrlQuery params)
{
post_data = "";
QUrl url(sci.getHost() + file_name);
url.setQuery(params);
data.clear();
data.append(params.toString().toUtf8());
request.setUrl(url);
nam->post(request, data);
}
From the Qt docs http://qt-project.org/doc/qt-5.1/qtnetwork/qnetworkaccessmanager.html
Note: After the request has finished, it is the responsibility of the
user to delete the QNetworkReply object at an appropriate time. Do not
directly delete it inside the slot connected to finished(). You can
use the deleteLater() function.
I would suggest calling reply->deleteLater() in your replyFinished() method.
You should call deleteLater() for an QNetworkReply object after use.
Note: After the request has finished, it is the responsibility of the user to delete the QNetworkReply object at an appropriate time. Do not directly delete it inside the slot connected to finished(). You can use the deleteLater() function.
More information here: http://harmattan-dev.nokia.com/docs/library/html/qt4/qnetworkaccessmanager.html
Thank you everyone for the hel.
It was very simply the "deleteLater()" on the reply in the
void replyFinished(QNetworkReply *reply)
{
}
and it should have looked like this
void replyFinished(QNetworkReply *reply)
{
// after all of your processing
reply->deleteLater();
}
it was such a small problem but one that drove me crazy for a long time so i hope this helps