Results 1 to 3 of 3

Thread: QImage deconstructor or delete / proper usage

  1. #1
    Join Date
    Jul 2015
    Posts
    1
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default QImage deconstructor or delete / proper usage

    I have a serious memory leak due to what I beleive to be a new instance of QImage being created but not deleted in the memory every time a function is called. My code looks somewhat like this.

    Qt Code:
    1. //myClass.cpp
    2.  
    3. // Public Function, called iterativly each frame
    4. void myClass::updateAndPaint(int **arr, float **arr2, int arrSize, int arr2Size, char type)
    5. {
    6. // Do stuff here
    7. update();
    8. }
    9.  
    10. // Private function
    11. void myClass::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
    12. {
    13. QRectF rec = boundingRect();
    14. QImage image(width, height, QImage::Format_RGB32);
    15.  
    16. // Do stuff here
    17. image.setPixel(x, y, thisPixel);
    18. // Do stuff here
    19.  
    20. painter->drawImage(rec, image);
    21. }
    To copy to clipboard, switch view to plain text mode 

    However if I make the QImage declaration global in the .h file like so
    Qt Code:
    1. // myClass.h
    2. QImage *image;
    To copy to clipboard, switch view to plain text mode 

    Then add this to the constructor
    Qt Code:
    1. myClass::myClass(int height, int width)
    2. {
    3. image = new QImage(width, height, QImage::Format_RGB32);
    4. // the rest of my constructor
    5. }
    To copy to clipboard, switch view to plain text mode 

    At any reference of the QImage object `image` in the paint function the compiler throws an error as such
    error C2228: left of '.setPixel' must have class/struct/union
    How instead should I be using QImage?
    Last edited by chrisbay90; 14th July 2015 at 07:21.

  2. #2
    Join Date
    Jul 2008
    Location
    Germany
    Posts
    507
    Thanks
    11
    Thanked 76 Times in 74 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QImage deconstructor or delete / proper usage

    Hi, if you use a pointer, you need to use image->setPixel().
    But your original code should not create a memory leak because the QImage is created on the stack and gets destroyed once the method myClass:aint() is left.

    Ginsengelf

  3. #3
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QImage deconstructor or delete / proper usage

    I'll make a pretty good bet that it is the "do stuff here" code that causes the memory leaks unless

    Qt Code:
    1. image = new QImage(width, height, QImage::Format_RGB32);
    To copy to clipboard, switch view to plain text mode 

    this code also ended up inside the paint() method.

Similar Threads

  1. Proper way to delete a QMap
    By jano_alex_es in forum Newbie
    Replies: 4
    Last Post: 28th February 2020, 17:31
  2. Proper QList usage
    By smick in forum Newbie
    Replies: 3
    Last Post: 25th September 2011, 15:51
  3. QSqlQuery prepared statements proper usage
    By psih128 in forum Qt Programming
    Replies: 5
    Last Post: 11th April 2011, 23:10
  4. Proper QList usage
    By space_otter in forum Newbie
    Replies: 5
    Last Post: 22nd June 2010, 06:57
  5. Replies: 1
    Last Post: 13th November 2009, 01:45

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.