Hello all!

I have been trying to pinpoint a heavy memory leak in a realtime network library I wrote, without success. As I transmit quite a lot of data (5-10MB/sec) the leak quickly crashes the application. The weird thing is that in about half of the program starts (without recompiling, just restarting!) no leaking occurs, at all! This happens both under windows (mingw/Qt4.71, msvc2008/Qt4.73) and under linux (g++/4.70).

After a lot of code rechecking and doing most of the memory allocations directly, my programm shows the following valgrind output:

Qt Code:
  1. ==2784== 327,763,678 bytes in 398 blocks are possibly lost in loss record 284 of 284
  2. ==2784== at 0x4025BD3: malloc (vg_replace_malloc.c:236)
  3. ==2784== by 0x4D6E05C: qMalloc(unsigned int) (qmalloc.cpp:55)
  4. ==2784== by 0x4D76A34: QByteArray::resize(int) (qbytearray.cpp:1379)
  5. ==2784== by 0x4C8D5B9: QRingBuffer::reserve(int) (qringbuffer_p.h:187)
  6. ==2784== by 0x4C8793C: QAbstractSocketPrivate::readFromSocket() (qabstractsocket.cpp:1158)
  7. ==2784== by 0x4C87F4B: QAbstractSocketPrivate::canReadNotification() (qabstractsocket.cpp:614)
  8. ==2784== by 0x4C74A7A: QAbstractSocketEngine::readNotification() (qabstractsocketengine.cpp:154)
  9. ==2784== by 0x4C75CEE: QReadNotifier::event(QEvent*) (qnativesocketengine.cpp:1103)
  10. ==2784== by 0x41B7FFB: QApplicationPrivate::notify_helper(QObject*, QEvent*) (qapplication.cpp:4396)
  11. ==2784== by 0x41BED92: QApplication::notify(QObject*, QEvent*) (qapplication.cpp:3798)
  12. ==2784== by 0x4E8D68A: QCoreApplication::notifyInternal(QObject*, QEvent*) (qcoreapplication.cpp:732)
  13. ==2784== by 0x4EBDA12: socketNotifierSourceDispatch(_GSource*, int (*)(void*), void*) (qcoreapplication.h:215)
To copy to clipboard, switch view to plain text mode 
I'm using a standard QTcpSocket.

Qt Code:
  1. void QntsBlockStreamReader::setupDataTCP()
  2. {
  3. nextBlockSize = 0;
  4. dataPortTCP = new QTcpSocket();
  5. connect(dataPortTCP,SIGNAL(connected()),this,SLOT(dataPortConnectedTCP()));
  6. connect(dataPortTCP,SIGNAL(readyRead()),this,SLOT(dataIncomingTCP()));
  7. dataPortTCP->connectToHost(writerAddress,dataPortNo);
  8. }
To copy to clipboard, switch view to plain text mode 
In it's readyRead slot, I create block objects - which I checked are properly released after usage.

Qt Code:
  1. void QntsBlockStreamReader::dataIncomingTCP()
  2. {
  3. while (dataPortTCP->bytesAvailable() > 0)
  4. {
  5. if (nextBlockSize == 0) {
  6. if (dataPortTCP->bytesAvailable() < (sizeof(quint8)+sizeof(TBlockSize))) return;
  7. // BlockSize contains the blockSize value itself
  8. QByteArray b = dataPortTCP->peek(sizeof(quint8)+sizeof(TBlockSize));
  9. QntsDataStream ds(&b,QIODevice::ReadOnly);
  10. quint8 protocolVersion;
  11. ds >> protocolVersion >> nextBlockSize;
  12. }
  13. if ((TBlockSize)dataPortTCP->bytesAvailable() < nextBlockSize) return;
  14. logMsg(QString("Bytes Available: %1 - %2").arg(dataPortTCP->bytesAvailable()).arg(QntsBlock::allocatedBlocks));
  15. QByteArray newData = dataPortTCP->read(nextBlockSize);
  16. blockArrived(new QntsBlock(newData),dataPortTCP->peerAddress(),dataPortTCP->peerPort());
  17. nextBlockSize = 0;
  18. }
  19. }
  20.  
  21. QntsBlock::QntsBlock(const QByteArray& ba)
  22. {
  23. m_size = ba.size();
  24. m_data = (char*)malloc(m_size);
  25. memcpy(&m_data[0],(void*)ba.constData(),m_size);
  26. allocatedBlocks += 1;
  27. }
  28.  
  29. QntsBlock::~QntsBlock()
  30. {
  31. allocatedBlocks -= 1;
  32. free(m_data);
  33. }
To copy to clipboard, switch view to plain text mode 
I would greatly appreciate any clues or hints how to proceed! First of all I would like to make sure, that I am not doing something plainly stupid, before investing even more time boiling this down, by creating a small test app, that shows the same symptoms.

Thx a lot in advance!

Johannes