Fix memleak in QBinaryJson::to{Raw,Binary}Data()


In Qt 5, the code belonged to QJsonDocument, and the memory was managed
by QJsonDocumentPrivate, which correctly cleaned it up in the d-tor.

However, when moved to Qt5Compat in Qt 6, the code simply became a
namespace with a bunch of free functions, so the underlying d-ptr is
gone. As a result, QBinaryJson::toRawData() simply hands a pointer to
the user, and it's now their responsibility to free the memory.
In case of QBinaryJson::toBinaryData(), the pointer is used to construct
a QByteArray (which did another copy), and was never freed afterwards.
That's a memory leak on the Qt side.

This patch fixes the memleak in QBinaryJson::toBinaryData(), and also
documents that the user has to free the memory obtained from
QBinaryJson::toRawData(). It also adds a qWarning() to the toRawData()
function, to steer the users towards toBinaryData() usage.
A follow-up patch will deprecate toRawData() immediately in 6.12.

Since toRawData() was used in toBinaryData() implementation, the
patch introduces a new static toRawDataHelper() that actually contains
all the logic.

The tests are also fixed to avoid memory leaks.

Amends the introduction of the code in Qt5Compat repo (Qt 6.0).

Fixes: QTBUG-149054
Pick-to: 6.12 6.11 6.8 6.5
Change-Id: If376d19f9788c753e29fd237abf7849307491504
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
commit82604842849a66554baac66ef4f4a4469f62d848
parent31877d54da29fa862fc17918c61ed43fd201eb19
authorIvan Solovev <ivan.solovev@qt.io>
committerIvan Solovev <ivan.solovev@qt.io>
commit timeMon, 17 Aug 2026 08:07:18 +0000