Results 1 to 6 of 6

Thread: UI and dynamic memory allocation, use of parent and child

  1. #1
    Join Date
    Jan 2013
    Posts
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default UI and dynamic memory allocation, use of parent and child

    Hey There,

    This might be more of a general C++ question so apologies in advanced if it is.

    I don't have a problem with my code working, but I am just wondering about memory leaking. In my code below I declare both *sym and *myPlotMarker into dynamic memory, so when my setupgraph has finished the symbols and plotmarkers are not deleted.

    1. Since I loose scope of the variable sym, this is classified as a memory leak correct?
    2. Is there a way I can use parent / child with these type of dynamic definitions so this "memory leak" doesn't occur? I cannot delete the *sym pointer as if I do it within scope, then it becomes irrelevant, and I can't do it in my mainWindow class destructor as it is out of scope.


    Qt Code:
    1. void myMainWindow::setupGraph()
    2. {
    3. //sets up the graph with the size of the windows.
    4.  
    5. double min_axis= -100;
    6. double max_axis= 100;
    7. double step_axis = 100;
    8. int y_axis_id = 0;
    9. int x_axis_id = 2;
    10.  
    11. ui->plotDisplayWiiOutput->autoRefresh();
    12. ui->plotDisplayWiiOutput->autoReplot();
    13. ui->plotDisplayWiiOutput->setAxisScale(y_axis_id,min_axis,max_axis,step_axis);
    14. ui->plotDisplayWiiOutput->setAxisScale(x_axis_id,min_axis,max_axis,step_axis);
    15. ui->plotDisplayWiiOutput->replot();
    16.  
    17. QwtSymbol *sym = new QwtSymbol;
    18. sym->setStyle(QwtSymbol::XCross);
    19. sym->setSize(20);
    20.  
    21. myPlotMarker = new QwtPlotMarker;
    22. myPlotMarker->setSymbol(sym);
    23.  
    24. }
    To copy to clipboard, switch view to plain text mode 

    I have this type of declaration right through my program so I am concerned that the memory leak might cause problems at some point. Here is another instance, if I try and delete the item before I close my popup window it would be pointless.

    Qt Code:
    1. for (i=0; i< NumberOfBins; i++)
    2. {
    3. QTableWidgetItem *mWidItem = new QTableWidgetItem(QString("%1 to %2 HZ")
    4. .arg(integratedFFT[i].interval.minValue())
    5. .arg(integratedFFT[i].interval.maxValue()));
    6. myTableWidget->setItem(i,0,mWidItem);
    7. }
    To copy to clipboard, switch view to plain text mode 

    So how do people handle this scenario. Do they globally initiate their pointers (in the header) like myPlotMarker in my example and clean them up manually in the destructor at the end of the program/object use. Or just not worry about it as all modern operating systems clean up the memory when the program exists?

    Thanks for your help in advance.

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: UI and dynamic memory allocation, use of parent and child

    As for the QTableWidget: It the documentation for QTableWidget::setItem() it is said that "The table takes ownership of the item.". That means when you delete the table widget, it will automatically delete all item. So no memory leak. I guess it is the same with QwtPlotMarker. See also the documentation about parent/child relations of QObject classes. Qt will delete the children automatically.

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: UI and dynamic memory allocation, use of parent and child

    If you have doubts such as this, it is always easiest to subclass the class, reimplement the destructor and output some message there. Then you can check yourself when the destructor is called if at all. Of course instead of that you can just set a breakpoint in the destructor of the original class.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  4. #4
    Join Date
    Jan 2013
    Posts
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: UI and dynamic memory allocation, use of parent and child

    Thanks for your help guys,

    @Lykurg, the QwtPlotMarker reference materials states "The QwtPlotMarker::setSymbol() member assigns a symbol to the marker. The symbol is drawn at the specified point." so I assume that means now the plotmarker is the parent/owns the symbol and so when the marker is destroyed, it has a flow on effect to the symbol. I get a bit confused with the assigning/owning/parent terminology. I declared myPlotMarker in the header file so it has the scope of the entire class, hence I deleted it in the destructor. Reading through your reference material of QObjects, I will go through and look at QObject::findChildren & QObject::parent to make sure this parent/child relationship is occurring.

    It seems as though a lot of my dynamically declared pointers are used by "parent/master" classes, so I think I have a lot less problems with deleting them than I originally thought.

    @wysota, had a look into subclassing (same as inheriting in my mind). Do you mean create a dummy class with the only difference between that and the original class being the extra destructor message. Thanks for that suggestion.

    Q1) It seems I intend to not destroy the majority of my dynamically pointed objects until the program finishes. And what I read is that on all modern OS that memory is reclaimed any way. So is all this memory leak stuff more of a best practice rather than an actual problem?

    Q2) Does QT creator have any warning/error messages to detect memory leakage, or an automated way I can monitor this (I guess an alternative to your suggestion @wysota).

    Again, thanks for your help team.

  5. #5
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: UI and dynamic memory allocation, use of parent and child

    Qt and the C++ STL provide smart pointer classes for handling heap-allocated items that are not otherwise tracked. QSharedPointer is one example.

    Q1. If your program only runs for short periods and does not exhaust system memory in that time then cleanup at termination is adequate but lazy. If you expect you program to continue running for days, weeks, under heavy load etc. then memory use will continue to rise until the system is exhausted of allocatable memory for the process: then bad things happen to your process and possibly others on the machine. In short, do not rely on clean up at exit; it is a bad habit to adopt.

    Q2. Tools like valgrind on Linux, or any number of instrumented memory allocators (malloc() drop-in replacements).

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: UI and dynamic memory allocation, use of parent and child

    @Q1: Also there is the aspect of maintenance. If at some day your current application grows and becomes just a part of a larger application, current laziness will require to go over the whole code looking for potential memory leaks. It is much easier to fix them as you create them than to go back and inspect the whole code after you have already forgotten what it was doing.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. memory allocation problem
    By marc2050 in forum Newbie
    Replies: 7
    Last Post: 23rd May 2011, 09:05
  2. Widget Memory Allocation
    By ArlexBee-871RBO in forum Qt Programming
    Replies: 5
    Last Post: 9th May 2010, 19:51
  3. QDrag : memory allocation
    By kghose in forum Qt Programming
    Replies: 1
    Last Post: 14th August 2008, 22:57
  4. limit memory allocation
    By magland in forum General Programming
    Replies: 10
    Last Post: 23rd March 2007, 09:21
  5. vector memory allocation
    By TheKedge in forum General Programming
    Replies: 1
    Last Post: 23rd March 2006, 17:27

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.