Pokered Save Editor 2
Pokemon Red & Blue save file editor - Qt 6 C++/QML
Loading...
Searching...
No Matches
qmlownership.h File Reference

qmlCppOwned() – protect Q_INVOKABLE QObject returns from QML's GC. More...

#include <QQmlEngine>
Include dependency graph for qmlownership.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

template<typename T>
static T * qmlCppOwned (T *obj)
 Hand QML CppOwnership of a C++-owned QObject returned from a Q_INVOKABLE.

Detailed Description

qmlCppOwned() – protect Q_INVOKABLE QObject returns from QML's GC.

Definition in file qmlownership.h.

Function Documentation

◆ qmlCppOwned()

template<typename T>
T * qmlCppOwned ( T * obj)
inlinestatic

Hand QML CppOwnership of a C++-owned QObject returned from a Q_INVOKABLE.

Q_INVOKABLE (and slot) returns of a parentless QObject default to JavaScriptOwnership, so QML's garbage collector frees them once the QML-side reference is dropped (e.g. a details editor closes). That leaves a dangling pointer in the owning C++ container -> use-after-free crash on the next access. These savefile objects are owned by their container in C++ for its lifetime, so QML must never delete them. (Q_PROPERTY returns do NOT need this – QML already assumes CppOwnership for those.)

Wrap any "...At()"-style Q_INVOKABLE return: return qmlCppOwned(vec.at(ind));

Parameters
objThe C++-owned object being returned to QML (may be nullptr).
Returns
obj unchanged, now pinned to C++ ownership.
See also
qt6-patterns.md ("Q_PROPERTY returns are safe; Q_INVOKABLE returns are NOT").

Definition at line 43 of file qmlownership.h.

Referenced by Storage::boxAt(), AreaMap::connAt(), PokemonStorageModel::getBoxMon(), PokemonStorageModel::getPartyMon(), AreaPokemon::grassMonsAt(), ItemStorageBox::itemAt(), PokemonBox::movesAt(), PlayerPokemon::partyAt(), HoFRecord::pokemonAt(), PokemonStorageBox::pokemonAt(), HallOfFame::recordAt(), AreaSign::signAt(), AreaSprites::spriteAt(), AreaWarps::warpAt(), and AreaPokemon::waterMonsAt().