Memory leakage when sending image from qml to c++
-
We have Qquickitem class to display the OpenGL window in Qml
Display::Display()
{
setFlag(QQuickItem::ItemHasContents);
}void Display::setImage(QImage image)
{
im = image;
imageUpdadte = true;
update();
}QSGNode* Display::updatePaintNode(QSGNode* oldNode, QQuickItem::UpdatePaintNodeData* updatePaintNodeData)
{
QSGSimpleTextureNode* node = static_cast<QSGSimpleTextureNode*>(oldNode);
if (!node) {
node = new QSGSimpleTextureNode();QSGTexture* texture = window()->createTextureFromImage(im); node->markDirty(QSGNode::DirtyForceUpdate); node->setTexture(texture); node->setRect(boundingRect()); } if(imageUpdadte==true) { //QImage im1("D:/S[0]_20201002_203942_848.png"); QSGTexture* texture = window()->createTextureFromImage(im); node->markDirty(QSGNode::DirtyForceUpdate); node->setTexture(texture); node->setRect(boundingRect()); imageUpdadte = false; qDebug() << "updatepaintImage"; } return node;
}
In Main.cpp we have registered as
qmlRegisterType<Display>("display",1,0,"Display");We have a helper class which contains image
QImage Helper::loadImage()
{
return m_loadImage;
}Qml Code to display the OpenGL window
import display 1.0
Window {
visible: true
width: 640
height: 480
title: qsTr("Hello World")
Rectangle {
width: parent.width
height: parent.height
clip: true
border.color: "red"Display { id: dpid objectName: "dpobj" width: parent.width height: parent.height } } Button { text: "press" onClicked: { dpid.setImage(helper.loadImage) } }
}
We will be getting a continues image to update in OpenGL window. When ever we click the button or new image comes the image gets updated in OpenGL window perfectly.
But the issue is, there is a memory leakage, when ever a new image is sent from qml to C++, on every new image sent the memory gets added up, the memory is not cleared before every new image.
We could visualize that on task manager.
We are using 32 bit msvc compiler.
Could any one help us to overcome from this memory leakage issue?
Also please suggest is there any other way in registering the Display class and to send the image to Display class directly? -
@gowdamnh said in Memory leakage when sending image from qml to c++:
QSGTexture* texture = window()->createTextureFromImage(im);
node->markDirty(QSGNode::DirtyForceUpdate);
node->setTexture(texture);You should
delete
the old texture when you set a new one.auto old = node->texture(); QSGTexture* texture = window()->createTextureFromImage(im); node->markDirty(QSGNode::DirtyForceUpdate); node->setTexture(texture); delete old;
I don't know what your exact use case is, but in general it should be possible and easier to use:
- just plain Image component from QML
- OR you can use ImageProvider https://doc.qt.io/qt-5/qquickimageprovider.html
then you don't have to subclass QQuickItem.
-
There will be a image coming from camera, which needs to be displayed in qml through opengl (Display class).
We will be doing some painting activity for image in opengl (Display class).
We have imported the registered Display class in main.qml (import display 1.0),
To display the opengl window in main.qml as well as to send the image to opengl (Display class)When ever we click the button "press", the image is sent to opengl(Display class) and it is displayed and it is working fine.
But the issue is after every new click, after every new image sent to opengl(Display class).
There is a memory leakage, that is memory is not released after every new image.
We can visualize this in "Task Manager" (Memory gets added up).I tried deleting the old texture, but even though the issue exist.
we need help to over come this issue.