Originally Posted by
d_stranz
- create the QImage as a stack variable (not a pointer variable) in the thread class
(My C++ programming days date to about 1999, so there's a whole lot of refreshing memory going on, and not the virtual kind, so this may be digging my hole deeper still...).
OK, but...
The Mandelbrot example is not allocating on the stack either. It does:
QImage image(resultSize, QImage::Format_RGB32);
To copy to clipboard, switch view to plain text mode
inside the run(), and then
emit renderedImage(image, scaleFactor);
emit renderedImage(image, scaleFactor);
To copy to clipboard, switch view to plain text mode
(With the renderedImage() declared as passing the QImage as &image)
and then in the main thread uses it directly:
pixmap
= QPixmap::fromImage(image
);
pixmap = QPixmap::fromImage(image);
To copy to clipboard, switch view to plain text mode
Originally Posted by
d_stranz
This is your memory leak. Your signal passes a reference to the QImage back to the main thread, not a pointer, and this new allocation never gets deleted.
I get why that would leak, but not why I can't delete it. If I'm passing a reference to the QImage, then deleting against that - why does it crash (and why only if it is passed up through the tread and not by a direct call)? Isn't the reference to the QImage I passed out a reference that preserves it from automatic deletion, and shouldn't it be available for a manual delete?
As I mentioned in the second posting I found a way to do it more efficiently by just letting it (suitably protected by mutex) update the main thread's data structure directly from the thread, and just emit a signal to say it's ready. But I remain a bit confused why the program above doesn't allow the delete without crashing?
Bookmarks