Fix memory leak issues in minitor page

Issue was that QT (incorrectly?) asserted that there was a binding loop between width and height in the `NetworkMJPGImage` component. This caused the height to evalualte to `infinity`, making QT create a buffer with an infinite amount of memory. Solved by calculating a serpeate `img_scale_factor` which both the width and height uses.

CURA-11180
This commit is contained in:
c.lamboo 2023-10-19 11:53:41 +02:00
parent e5e941aeba
commit 0e1262126a
2 changed files with 24 additions and 15 deletions

View file

@ -1,6 +1,8 @@
# Copyright (c) 2018 Aldo Hoeben / fieldOfView # Copyright (c) 2018 Aldo Hoeben / fieldOfView
# NetworkMJPGImage is released under the terms of the LGPLv3 or higher. # NetworkMJPGImage is released under the terms of the LGPLv3 or higher.
from typing import Optional
from PyQt6.QtCore import QUrl, pyqtProperty, pyqtSignal, pyqtSlot, QRect, QByteArray from PyQt6.QtCore import QUrl, pyqtProperty, pyqtSignal, pyqtSlot, QRect, QByteArray
from PyQt6.QtGui import QImage, QPainter from PyQt6.QtGui import QImage, QPainter
from PyQt6.QtQuick import QQuickPaintedItem from PyQt6.QtQuick import QQuickPaintedItem
@ -19,9 +21,9 @@ class NetworkMJPGImage(QQuickPaintedItem):
self._stream_buffer = QByteArray() self._stream_buffer = QByteArray()
self._stream_buffer_start_index = -1 self._stream_buffer_start_index = -1
self._network_manager = None # type: QNetworkAccessManager self._network_manager: Optional[QNetworkAccessManager] = None
self._image_request = None # type: QNetworkRequest self._image_request: Optional[QNetworkRequest] = None
self._image_reply = None # type: QNetworkReply self._image_reply: Optional[QNetworkReply] = None
self._image = QImage() self._image = QImage()
self._image_rect = QRect() self._image_rect = QRect()

View file

@ -1,4 +1,4 @@
// Copyright (c) 2019 Ultimaker B.V. // Copyright (c) 2023 UltiMaker
// Cura is released under the terms of the LGPLv3 or higher. // Cura is released under the terms of the LGPLv3 or higher.
import QtQuick 2.2 import QtQuick 2.2
@ -6,7 +6,7 @@ import UM 1.3 as UM
import Cura 1.0 as Cura import Cura 1.0 as Cura
Item { Item {
property var cameraUrl: ""; property string cameraUrl: "";
Rectangle { Rectangle {
anchors.fill:parent; anchors.fill:parent;
@ -34,22 +34,29 @@ Item {
Cura.NetworkMJPGImage { Cura.NetworkMJPGImage {
id: cameraImage id: cameraImage
anchors.horizontalCenter: parent.horizontalCenter; anchors.horizontalCenter: parent.horizontalCenter
anchors.verticalCenter: parent.verticalCenter; anchors.verticalCenter: parent.verticalCenter
height: Math.round((imageHeight === 0 ? 600 * screenScaleFactor : imageHeight) * width / imageWidth);
readonly property real img_scale_factor: {
if (imageWidth > maximumWidth || imageHeight > maximumHeight) {
return Math.min(maximumWidth / imageWidth, maximumHeight / imageHeight);
}
return 1.0;
}
width: imageWidth === 0 ? 800 * screenScaleFactor : imageWidth * img_scale_factor
height: imageHeight === 0 ? 600 * screenScaleFactor : imageHeight * img_scale_factor
onVisibleChanged: { onVisibleChanged: {
if (cameraUrl === "") return;
if (visible) { if (visible) {
if (cameraUrl != "") { start();
start();
}
} else { } else {
if (cameraUrl != "") { stop();
stop();
}
} }
} }
source: cameraUrl source: cameraUrl
width: Math.min(imageWidth === 0 ? 800 * screenScaleFactor : imageWidth, maximumWidth);
z: 1 z: 1
} }