From 57489437e0eb3057d34030325c784d6ac2dbf3fe Mon Sep 17 00:00:00 2001 From: Matt Ranostay Date: Mon, 30 Apr 2018 17:58:11 -0700 Subject: libqtappfw: message: fix memory leak with QJsonArray QJsonArray object was being allocated on the heap and never freed. Switch to having a m_request member field and confirming it is only populated with data once. Change-Id: I60f406eff032650cac878efa7e1db61ee18e2eb2 Signed-off-by: Matt Ranostay --- message.cpp | 16 ++++++++++------ message.h | 2 ++ 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/message.cpp b/message.cpp index 95bc586..33211da 100644 --- a/message.cpp +++ b/message.cpp @@ -29,14 +29,18 @@ Message::Message() bool Message::createRequest(QString api, QString verb, QJsonValue parameter) { - QJsonArray *request = new QJsonArray; - request->append(Call); - request->append(9999); - request->append(api + (QString)("/") + verb); - request->append(QJsonValue(parameter)); + if (!m_request.isEmpty()){ + qWarning("Message instance has already been used. Cannot send another request."); + return false; + } + + m_request.append(Call); + m_request.append(9999); + m_request.append(api + (QString)("/") + verb); + m_request.append(QJsonValue(parameter)); QJsonDocument jdoc; - jdoc.setArray(*request); + jdoc.setArray(m_request); m_jdoc = jdoc; m_init = true; diff --git a/message.h b/message.h index 64bb69f..37dc740 100644 --- a/message.h +++ b/message.h @@ -18,6 +18,7 @@ #define MESSAGE_H #include +#include #include #include @@ -92,6 +93,7 @@ class Message : public QObject protected: bool m_event, m_init, m_reply; QString m_event_api, m_event_name, m_reply_info, m_reply_status, m_reply_uuid; + QJsonArray m_request; QJsonDocument m_jdoc; QJsonObject m_event_data, m_reply_data; }; -- cgit 1.2.3-korg