Results 1 to 3 of 3

Thread: Is there a memory leak here?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Feb 2014
    Posts
    11
    Thanks
    2
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Is there a memory leak here?

    Hello, I have some code I wanted to make sure I have no memory leaks in it so I ran it on my mac and opened activity monitor to see how much memory it was using, then I kept opening new windows(Editor class mentioned in code below) and closing it, and the memory usage increased when I opened them and didn't decrease as I closed. I tried the same thing in top using terminal to get the same result. Is it some lack of knowledge on my behalf on how activity monitor, top and even memory usage works? is it normal? or is there a leak in the code below

    P.S. The Editor class allocates nothing dynamically (at least not more than child widgets and variables that were suppose to be disposed after the class is deleted right? ).

    add_editor (when a new window is opened):

    Qt Code:
    1. void MainControl::add_editor(int mode)
    2. {
    3. int i=0;
    4. QPointer<Editor> *editor_aux=new QPointer<Editor> [editor_n+1];
    5.  
    6. while(i<editor_n)
    7. {
    8. editor_aux[i]=editor_array[i];
    9. i++;
    10. }
    11.  
    12. editor_aux[i]=new Editor(i,mode);
    13. set_editor_conenctions(editor_aux[i]);
    14.  
    15. delete[] editor_array;
    16.  
    17. editor_array=editor_aux;
    18. editor_n++;
    19.  
    20. if(editor_array[i]->start_response==2)
    21. {
    22. del_editor(editor_array[i]->id);
    23. }
    24. }
    To copy to clipboard, switch view to plain text mode 

    del_editor (when the window is closed):

    Qt Code:
    1. void MainControl::del_editor(int id_arg)
    2. {
    3.  
    4. if(editor_n>1)
    5. {
    6. int i=0;
    7. int a=0;
    8.  
    9. QPointer<Editor> *editor_aux=new QPointer<Editor>[editor_n-1];
    10.  
    11. while(i<editor_n)
    12. {
    13. if(editor_array[i]->id==id_arg)
    14. {
    15. unset_editor_conenctions(editor_array[i]);
    16. delete editor_array[i];
    17. }else{
    18. editor_aux[a]=editor_array[i];
    19. a++;
    20. }
    21. i++;
    22. }
    23.  
    24. delete[] editor_array;
    25.  
    26. editor_array=editor_aux;
    27. editor_n--;
    28. }
    29.  
    30. //This below is not needed because when the last window is closed the program closes
    31. /*
    32.   if(editor_n==1){
    33.   editor_n=0;
    34.   delete editor_array[0];
    35.   }
    36.   */
    37. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Is there a memory leak here?

    Freed memory is not necessarily reclaimed by operating system right away.

    You could run you program through valgrind and see if its memcheck tool reports any leaks.

    Btw, your array handling code looks way more complicated than it needs to be, just use a resizable sequence container, e.g. a list or vector.

    Cheers,
    _

  3. #3
    Join Date
    Feb 2014
    Posts
    11
    Thanks
    2
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Is there a memory leak here?

    Thank you very much, I will look into valgrind and something more simple

    Cheers

Similar Threads

  1. memory leak - where?
    By Tomasz in forum Newbie
    Replies: 36
    Last Post: 5th September 2010, 23:47
  2. Memory leak
    By yxtx1984 in forum Qt Programming
    Replies: 4
    Last Post: 26th February 2010, 11:13
  3. Memory leak of Qt?
    By Sheng in forum Qt Programming
    Replies: 1
    Last Post: 1st April 2009, 23:32
  4. Memory leak?
    By Enygma in forum Qt Programming
    Replies: 10
    Last Post: 4th September 2007, 16:24
  5. Memory Leak in Qt
    By Krish_ng in forum Qt Programming
    Replies: 3
    Last Post: 22nd July 2007, 08:02

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.