Verified Commit a01f237c authored by Jonah Brüchert's avatar Jonah Brüchert 💬 Committed by Linus Jahn
Browse files

UploadManager: Avoid potential memory leak

parent 585b02cd
Pipeline #83596 passed with stage
in 2 minutes and 34 seconds
......@@ -85,24 +85,23 @@ void UploadManager::sendFile(const QString &jid, const QUrl &fileUrl, const QStr
const MessageType messageType = MediaUtils::messageType(mimeType);
const QString msgId = QXmppUtils::generateStanzaUuid();
auto *msg = new Message;
msg->setFrom(m_client->configuration().jidBare());
msg->setTo(jid);
msg->setId(msgId);
msg->setIsOwn(true);
msg->setBody(body);
msg->setMediaType(messageType);
msg->setStamp(QDateTime::currentDateTimeUtc());
msg->setMediaSize(file.size());
msg->setMediaContentType(mimeType.name());
msg->setMediaLastModified(file.lastModified());
msg->setMediaLocation(file.filePath());
Message msg;
msg.setFrom(m_client->configuration().jidBare());
msg.setTo(jid);
msg.setId(msgId);
msg.setIsOwn(true);
msg.setBody(body);
msg.setMediaType(messageType);
msg.setStamp(QDateTime::currentDateTimeUtc());
msg.setMediaSize(file.size());
msg.setMediaContentType(mimeType.name());
msg.setMediaLastModified(file.lastModified());
msg.setMediaLocation(file.filePath());
// cache message and upload
emit Kaidan::instance()->transferCache()->addJobRequested(msgId, upload->bytesTotal());
m_messages.insert(upload->id(), msg);
emit MessageModel::instance()->addMessageRequested(*msg, MessageOrigin::UserInput);
emit MessageModel::instance()->addMessageRequested(msg, MessageOrigin::UserInput);
m_messages[upload->id()] = std::move(msg);
connect(upload, &QXmppHttpUpload::bytesSentChanged, this, [=] () {
emit Kaidan::instance()->transferCache()->setJobBytesSentRequested(
......@@ -114,46 +113,46 @@ void UploadManager::handleUploadSucceeded(const QXmppHttpUpload *upload)
{
qDebug() << "[client] [UploadManager] A file upload has succeeded. Now sending message.";
Message *originalMsg = m_messages.value(upload->id());
auto &originalMsg = m_messages[upload->id()];
const QString oobUrl = upload->slot().getUrl().toEncoded();
const QString body = originalMsg->body().isEmpty()
const QString body = originalMsg.body().isEmpty()
? oobUrl
: originalMsg->body() + "\n" + oobUrl;
: originalMsg.body() + "\n" + oobUrl;
emit MessageModel::instance()->updateMessageRequested(originalMsg->id(), [=] (Message &msg) {
emit MessageModel::instance()->updateMessageRequested(originalMsg.id(), [=] (Message &msg) {
msg.setOutOfBandUrl(oobUrl);
});
// send message
QXmppMessage m(originalMsg->from(), originalMsg->to(), body);
m.setId(originalMsg->id());
QXmppMessage m(originalMsg.from(), originalMsg.to(), body);
m.setId(originalMsg.id());
m.setReceiptRequested(true);
m.setStamp(originalMsg->stamp());
m.setStamp(originalMsg.stamp());
m.setOutOfBandUrl(upload->slot().getUrl().toEncoded());
bool success = m_client->sendPacket(m);
if (success) {
emit MessageModel::instance()->updateMessageRequested(originalMsg->id(), [](Message &msg) {
emit MessageModel::instance()->updateMessageRequested(originalMsg.id(), [](Message &msg) {
msg.setDeliveryState(Enums::DeliveryState::Sent);
msg.setErrorText({});
});
} else {
emit Kaidan::instance()->passiveNotificationRequested(tr("Message could not be sent."));
emit MessageModel::instance()->updateMessageRequested(originalMsg->id(), [](Message &msg) {
emit MessageModel::instance()->updateMessageRequested(originalMsg.id(), [](Message &msg) {
msg.setDeliveryState(Enums::DeliveryState::Error);
msg.setErrorText(QStringLiteral("Message could not be sent."));
});
}
m_messages.remove(upload->id());
emit Kaidan::instance()->transferCache()->removeJobRequested(originalMsg->id());
emit Kaidan::instance()->transferCache()->removeJobRequested(originalMsg.id());
m_messages.erase(upload->id());
}
void UploadManager::handleUploadFailed(const QXmppHttpUpload *upload)
{
qDebug() << "[client] [UploadManager] A file upload has failed.";
const QString &msgId = m_messages.value(upload->id())->id();
m_messages.remove(upload->id());
const QString &msgId = m_messages[upload->id()].id();
m_messages.erase(upload->id());
emit Kaidan::instance()->transferCache()->removeJobRequested(msgId);
}
......@@ -32,9 +32,11 @@
// Qt
#include <QObject>
#include <QMap>
class Message;
#include <unordered_map>
#include "Message.h"
class RosterManager;
class QXmppClient;
class QXmppHttpUpload;
......@@ -79,5 +81,5 @@ private:
QXmppUploadManager *m_manager;
RosterManager *m_rosterManager;
QMap<int, Message*> m_messages;
std::unordered_map<int, Message> m_messages;
};
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment