Results 1 to 3 of 3

Thread: Any reason for memory leak?

  1. #1
    Join Date
    May 2007
    Posts
    301
    Thanks
    46
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Any reason for memory leak?

    Hi,

    For some reason, when checking the physical memory in task manager when the thread below is running, the memory keeps going down, I cannot understand why?

    Qt Code:
    1. void CanRead::run()
    2. {
    3. m_nCount = 0;
    4.  
    5. m_bCancelled = false;
    6. unsigned long nMsgAmount = 2;
    7.  
    8. CANDATA can[MAX];
    9.  
    10.  
    11. while( !m_bCancelled )
    12. {
    13.  
    14. unsigned long numMsgs = 0;
    15. PASSTHRU_MSG *pRxMsg = NULL;
    16. pRxMsg = theApp->GetMessageDetail( &numMsgs, 0 );
    17. nMsgAmount = numMsgs; // store amount of messages we got
    18. int index = 0;
    19. // Read can data, store in buffer ( read say N amount and then emit? )
    20. while ( numMsgs && !m_bCancelled )
    21. {
    22. QString szTemp = "";
    23. can[index].strData = "";
    24. unsigned int iCanId = 0;
    25. for ( int iLoop = 0; iLoop < 4; iLoop++)
    26. {
    27. iCanId = (iCanId << 8 ) | pRxMsg->Data[iLoop];
    28. }
    29. if( pRxMsg->RxStatus & 0x100 )
    30. can[index].strId.sprintf( "%X X", iCanId);
    31. else
    32. can[index].strId.sprintf( "%03X", iCanId);
    33.  
    34. for ( int iLoop = 4; (unsigned)iLoop < (pRxMsg->DataSize ); iLoop++)
    35. {
    36. szTemp.sprintf("%02X ", pRxMsg->Data[iLoop]);
    37. can[index].strData += szTemp;
    38. }
    39.  
    40. can[index].strTime.sprintf("%04d", pRxMsg->Timestamp);
    41.  
    42. numMsgs--;
    43.  
    44. m_nCount++;
    45.  
    46. } // end while ( ulNoMsgs )
    47.  
    48.  
    49. msleep(1);
    50.  
    51. } // end while ( true )
    52.  
    53. }
    To copy to clipboard, switch view to plain text mode 

    I thought it might have been the call to GetMessageDetail, but commenting this out and the memory still leaks?

    Anyone any ideas?

    Kind regards,
    Steve

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Any reason for memory leak?

    Are you sure it is not GetMessageDetail? What if you delete pRxMsg at the end of the first while loop?

    If it is not GetMessageDetail, try running without the thread, and see if the mem increases then.

    Regards

  3. #3
    Join Date
    May 2007
    Posts
    301
    Thanks
    46
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Any reason for memory leak?

    Thanks Marcel,

    It was me being stupid, I was continually adding an item to a listwidget which was causing it - I shouldn't have been doing that from within another thread except the main GUI one.

    Regards,
    Steve

Similar Threads

  1. Memory Leak in my Application :-(
    By Svaths in forum Qt Programming
    Replies: 4
    Last Post: 27th July 2007, 19:42

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.