qt: fix leak of network resources from RoundImage

also add documentation to address ambiguity in object ownership
This commit is contained in:
Prince Gupta 2024-03-11 12:34:32 +05:30 committed by Jean-Baptiste Kempf
parent cc922574c6
commit dafc146cf8
1 changed files with 27 additions and 1 deletions

View File

@ -104,7 +104,15 @@ namespace
class ImageReader : public AsyncTask<QImage> class ImageReader : public AsyncTask<QImage>
{ {
public: public:
// requestedSize is only taken as hint, the Image is resized with PreserveAspectCrop /**
* @brief ImageReader
* @param device i/o source to read from, ImageReader doesn't take owner ship of the device
* parent must make sure availability of device through out the lifetime of this instance
*
* @param requestedSize only taken as hint, the Image is resized with PreserveAspectCrop
*
* @param radius
*/
ImageReader(QIODevice *device, QSize requestedSize, const qreal radius) ImageReader(QIODevice *device, QSize requestedSize, const qreal radius)
: device {device} : device {device}
, requestedSize {requestedSize} , requestedSize {requestedSize}
@ -182,11 +190,19 @@ namespace
class NetworkImageResponse : public QQuickImageResponse class NetworkImageResponse : public QQuickImageResponse
{ {
public: public:
/**
* @brief NetworkImageResponse
* @param reply - Network reply to read from, NetworkImageResponse takes ownership of this object
* @param requestedSize - passed to ImageReader class
* @param radius - passed to ImageReader class
*/
NetworkImageResponse(QNetworkReply *reply, QSize requestedSize, const qreal radius) NetworkImageResponse(QNetworkReply *reply, QSize requestedSize, const qreal radius)
: reply {reply} : reply {reply}
, requestedSize {requestedSize} , requestedSize {requestedSize}
, radius {radius} , radius {radius}
{ {
reply->setParent(this);
QObject::connect(reply, &QNetworkReply::finished QObject::connect(reply, &QNetworkReply::finished
, this, &NetworkImageResponse::handleNetworkReplyFinished); , this, &NetworkImageResponse::handleNetworkReplyFinished);
} }
@ -216,6 +232,8 @@ namespace
{ {
error = reply->errorString(); error = reply->errorString();
emit finished(); emit finished();
releaseNetworkReply();
return; return;
} }
@ -226,12 +244,20 @@ namespace
error = reader->errorString(); error = reader->errorString();
reader.reset(); reader.reset();
releaseNetworkReply();
emit finished(); emit finished();
}); });
reader->start(*QThreadPool::globalInstance()); reader->start(*QThreadPool::globalInstance());
} }
void releaseNetworkReply()
{
reply->deleteLater();
reply = nullptr;
}
QNetworkReply *reply; QNetworkReply *reply;
QSize requestedSize; QSize requestedSize;
qreal radius; qreal radius;