Results 1 to 5 of 5

Thread: memory leakage problem

  1. #1
    Join Date
    Feb 2011
    Posts
    31
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    23

    Default memory leakage problem

    hello
    i am running into trouble with an application which work perfectly for some time, and the it locks as long as a number of iteration has been executed: i am suspecting memory leakage since this number is depending on the memory available on my (windows XP) PC: on machine A (2G RAM) it takes 12 to 18 iterations on average, on machine B (4 G RAM) it takes approx 50 iterations. When looking a t the Windows task manager I can also observe that memory is slowly growing as long as the number of iterations
    'Lock' happens as soon as the program 'start' a certain thread: 'lock' happens since thread never start (supposedly because the thead cannot get free memory to operate)

    I checked my code and
    - storage is either static or 'automatic' (variable decalred inside the functions)
    - 'new' objects are seldomly declared and promptly 'deleted' when non necessary
    - *alloc functions are never used during the run time, but only during the initialization of the program (only one time!!)

    I suspect that the issue may be due to the fact that I have a thread which makes the UART communicating with the main application and using many 'emit': is this an issue? should i take any countermeasure for this?

    i am also looking for a way to 'profile' the application memory usage, please advice: Is there under visual studio a way to check / monitoring the memory and class allocation used by the program or thread?

    sorry for not posting any code example: the program is quite complex and probably will not help

    thanks in advance for your help
    Last edited by marco.stanzani; 9th March 2012 at 15:58.

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows
    Thanks
    21
    Thanked 418 Times in 411 Posts

    Default Re: memory leakage problem

    You can try this if you are using visual studio:
    http://vld.codeplex.com/

    Under linux you can use valgrind.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. The following 2 users say thank you to high_flyer for this useful post:

    marco.stanzani (10th March 2012), Zlatomir (16th April 2012)

  4. #3
    Join Date
    Feb 2011
    Posts
    31
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    23

    Default Re: memory leakage problem

    ok, think i have isolate the issue, which is not related to memory leakage (BTW, thanks for the advice, the suggested tool helped to tune the program)
    it looks like that every time i perform a 'connectToHost' the allocated memory (i am loking at the windows task manager) increase a bit (the program does this many times)
    I'll try to post some simple code example
    thanks in advance to who'll look into this

  5. #4
    Join Date
    Feb 2011
    Posts
    31
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    23

    Default Re: memory leakage problem

    another chapter to this story
    The memory leakage is due a combination threads and timers (can I use a Qtimer outside a Qthread?? this is the point: it seems a huge limitation of Qtimer indeed ...)

    Below the example code: the memory in growing everytime m_progSupply->start() is executing by the main (i can see this using the debugger but i cannot get into the code since this is a thread).
    I cannot see any reason for this: timer instance is correctly deleted as far as i can see

    Qt Code:
    1. main()
    2. {
    3. ....
    4. m_progSupply->start(); // progSupply is a parent class of Qthread
    5. .....
    6.  
    7. }
    8.  
    9. ---------------------------------------------------------------------------
    10. progSupply::progSupply(QWidget *parent, Qt::WFlags flags)
    11. : QThread(parent)
    12. {
    13. ....
    14. }
    15.  
    16. void progSupply::run()
    17. {
    18. m_UartTimer = new QTimer(this);
    19. connect(m_UartTimer, SIGNAL(timeout()), this, SLOT(TimeOutUart()), Qt::DirectConnection);
    20. m_UartTimer->setSingleShot(false);
    21. StartTimeUart();
    22.  
    23. exec();
    24. }
    25.  
    26.  
    27. void progSupply::StartTimeUart()
    28. {
    29. m_UartTimer->start(UART_TIMEOUT);
    30. }
    31.  
    32. void progSupply::TimeOutUart()
    33. {
    34. m_UartTimer->stop();
    35.  
    36. checksUart();
    37.  
    38. }
    39.  
    40. void progSupply::checksUart()
    41. {
    42. m_UartTimer->stop();
    43.  
    44. if (data == false) && (m_repeat == true)
    45. {
    46. StartTimeUart(); // re-arm timer
    47. return;
    48. }
    49. else
    50. {
    51. emit endProgrammerSignal("OK");
    52.  
    53. if (m_UartTimer)
    54. delete(m_UartTimer);
    55.  
    56. }
    57.  
    58. }
    To copy to clipboard, switch view to plain text mode 

  6. #5
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows
    Thanks
    21
    Thanked 418 Times in 411 Posts

    Default Re: memory leakage problem

    timer instance is correctly deleted as far as i can see
    Its a very bad idea to delete an object in a slot which it self invoked.
    Why are you dynamically allocating your timer?
    You stay in run() all the time because of exec() - so you can just use a local variable.
    You should also have a look at QMutex.
    And finally, what you see in the process manager regarding the memory allocation doesn't have to mean its a memory leak.
    The OS manages the memory for the processes, and doesn't allocate or free the memory 1:1 as you do it internally in your application - to save on cleaning/allocating operations.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

Similar Threads

  1. QMessageBox memory problem
    By Jeffb in forum Qt Programming
    Replies: 6
    Last Post: 8th January 2012, 11:09
  2. memory allocation problem
    By marc2050 in forum Newbie
    Replies: 7
    Last Post: 23rd May 2011, 09:05
  3. Memory Leakage problem.
    By bilalsaeed in forum Qt for Embedded and Mobile
    Replies: 9
    Last Post: 13th February 2011, 19:13
  4. Qt Memory size problem ?
    By nrabara in forum Newbie
    Replies: 1
    Last Post: 29th September 2008, 15:27
  5. Virtual memory problem
    By Rahul in forum Qt Programming
    Replies: 1
    Last Post: 24th October 2007, 13:29

Tags for this Thread

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
  •  
Qt is a trademark of The Qt Company.