QSerialBus - Memory leak?



  • 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() — do nothing special unless checking Connected state.
    Maximum queue 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.


  • Lifetime Qt Champion

    Hi,

    It looks like at some point your calling queue in a loop that doesn't let the event loop run properly. What about using a single shot QTimer to queue the next call to your queue function ?



  • @SGaist

    Yeah, I removed all queue() calls from loop, except !checkState() side and replace them to:

    connect(reply, &QModbusReply::destroyed, [this] { QTimer::singleShot(60, [this] { queue(); }); });
    

    and there are no leaks now. But less than 60 ms still causes leak, but it's not a problem.

    Thanks.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.