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.
//myClass.cpp
// Public Function, called iterativly each frame
void myClass::updateAndPaint(int **arr, float **arr2, int arrSize, int arr2Size, char type)
{
// Do stuff here
update();
}
// Private function
{
// Do stuff here
image.setPixel(x, y, thisPixel);
// Do stuff here
painter->drawImage(rec, image);
}
//myClass.cpp
// Public Function, called iterativly each frame
void myClass::updateAndPaint(int **arr, float **arr2, int arrSize, int arr2Size, char type)
{
// Do stuff here
update();
}
// Private function
void myClass::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
QRectF rec = boundingRect();
QImage image(width, height, QImage::Format_RGB32);
// Do stuff here
image.setPixel(x, y, thisPixel);
// Do stuff here
painter->drawImage(rec, image);
}
To copy to clipboard, switch view to plain text mode
However if I make the QImage declaration global in the .h file like so
// myClass.h
// myClass.h
QImage *image;
To copy to clipboard, switch view to plain text mode
Then add this to the constructor
myClass::myClass(int height, int width)
{
// the rest of my constructor
}
myClass::myClass(int height, int width)
{
image = new QImage(width, height, QImage::Format_RGB32);
// the rest of my constructor
}
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?
Bookmarks