Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QSerialBus - Memory leak?
Forum Updated to NodeBB v4.3 + New Features

QSerialBus - Memory leak?

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 2 Posters 1.8k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    someoneinthebox
    wrote on 15 Mar 2017, 08:18 last edited by
    #1

    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!

    Mom said I am special.

    S 1 Reply Last reply 15 Mar 2017, 09:25
    0
    • S someoneinthebox
      15 Mar 2017, 08:18

      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!

      S Offline
      S Offline
      someoneinthebox
      wrote on 15 Mar 2017, 09:25 last edited by someoneinthebox
      #2

      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.

      Mom said I am special.

      1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on 15 Mar 2017, 23:12 last edited by
        #3

        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 ?

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        S 1 Reply Last reply 16 Mar 2017, 06:14
        1
        • SGaistS SGaist
          15 Mar 2017, 23:12

          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 ?

          S Offline
          S Offline
          someoneinthebox
          wrote on 16 Mar 2017, 06:14 last edited by someoneinthebox
          #4

          @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.

          Mom said I am special.

          1 Reply Last reply
          0

          1/4

          15 Mar 2017, 08:18

          • Login

          • Login or register to search.
          1 out of 4
          • First post
            1/4
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved