Results 1 to 5 of 5

Thread: A problem with memory management

  1. #1

    Default A problem with memory management

    Hello.

    I have a problem with my QwtPlot-derived class:
    Qt Code:
    1. class Plot2D : public QwtPlot
    2. {
    3. public:
    4. Plot2D(QWidget *parent);
    5. ~Plot2D();
    6.  
    7. private:
    8. QwtPlotSpectrogram *meshGrid;
    9. SpectrogramData *meshData; // public : QwtRasterData,
    10. ColorMap *meshMap; // : public QwtColorMap
    11. // These two classes use their respective default destructors. No memory allocation takes place inside these classes.
    12. QwtScaleDraw *scaleDraw;
    13. QwtScaleWidget *rightAxis;
    14. };
    To copy to clipboard, switch view to plain text mode 
    Its constructor and destructor are set to:
    Qt Code:
    1. Plot2D::Plot2D(QWidget *parent) :
    2. QwtPlot(parent),
    3. meshGrid(new QwtPlotSpectrogram()),
    4. meshData(new SpectrogramData()),
    5. meshMap(new ColorMap()),
    6. scaleDraw(new QwtScaleDraw())
    7. {
    8. meshGrid->attach(this);
    9. meshGrid->setData(meshData);
    10. meshGrid->setColorMap(meshMap);
    11. rightAxis = axisWidget(QwtPlot::yRight);
    12. rightAxis->setColorBarEnabled(true);
    13. enableAxis(QwtPlot::yRight);
    14. setAxisScaleDraw(QwtPlot::yLeft,scaleDraw);
    15. }
    16. Plot2D::~Plot2D()
    17. {
    18. delete scaleDraw;
    19. delete meshMap;
    20. delete meshData;
    21. delete meshGrid;
    22. }
    To copy to clipboard, switch view to plain text mode 
    My program halts and eventually throws an error if I simply run it and subsequently close it (upon deleting the Plot2D object). This simple procedure is completed successfully if I comment out the bracketed instructions on either constructor or destructor. I'm guessing that there is something wrong with my implementation of memory management here. What might be the problem?

  2. #2
    Join Date
    Feb 2006
    Location
    Munich, Germany
    Posts
    3,309
    Thanked 879 Times in 827 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: A problem with memory management

    Like it is common in Qt programming Qwt classes usually take ownership of the objects they get assigned. But in opposite to QObject ownerships there is no mechanism to auto detect, when an object gets deleted from somewhere else.

    So to avoid that the objects are deleted twice simple remove all of the delete statements from your destructor.

    Uwe

  3. #3

    Default Re: A problem with memory management

    Oh, that makes sense then.
    One more question - am I in danger of creating a memory leak if I, for instance, have this kind of function?
    Qt Code:
    1. Plot2d::function_that_gets_called_a_lot_from_my_main_window()
    2. {
    3. meshGrid->setData(new SpectrogramData());
    4. rightAxis->setColorMap(zInterval, new ColorMap());
    5. // etc.
    6. // Basically where every function, that needs a pointer, gets a new() object.
    7. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Feb 2006
    Location
    Munich, Germany
    Posts
    3,309
    Thanked 879 Times in 827 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: A problem with memory management

    No - when you assign a new data or color map object the previously assigned one is internally deleted.

    Uwe

  5. #5

    Default Re: A problem with memory management

    Thanks, Uwe.

Similar Threads

  1. Memory management problem
    By zloigall in forum Newbie
    Replies: 4
    Last Post: 25th July 2012, 17:16
  2. Memory management in Qt?
    By wookoon in forum Qt Programming
    Replies: 7
    Last Post: 6th November 2010, 19:20
  3. Memory Management Reg
    By BalaQT in forum Newbie
    Replies: 10
    Last Post: 4th February 2010, 13:43
  4. Memory management
    By cyberboy in forum Qt Programming
    Replies: 2
    Last Post: 12th June 2008, 21:48
  5. Memory management in QT
    By Gayathri in forum Qt Programming
    Replies: 1
    Last Post: 17th November 2006, 08:21

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.