Commit 764fce4a authored by Harald Sitter's avatar Harald Sitter 🏳🌈
Browse files

make sure Solid::Battery is not deleted from QML

Summary:
Battery objects are casted DeviceInterface objects and those are
owned by Solid. deleting them outside solid means they will end
up as dangling pointers inside Solid's global static objects.

when switching away from the energy KCM the qml engine would
get cleaned up as part of the KCM destruction, qml would then
sweep up the Battery object and corrupt the solid internal
pointers.

to prevent this, explicitly mark Battery objects we give to QML
as owned on the c++ side.

BUG: 413003
FIXED-IN: 5.18.0

Test Plan:
open kinfocenter
switch to energy
switch away
switch to energy
no crash

Reviewers: broulik, davidedmundson

Reviewed By: davidedmundson

Subscribers: plasma-devel

Tags: #plasma

Differential Revision: https://phabricator.kde.org/D26725
parent b18b1988
......@@ -73,7 +73,12 @@ QVariant BatteryModel::data(const QModelIndex &index, int role) const
}
if (role == BatteryRole) {
return QVariant::fromValue(m_batteries.value(index.row()).as<Solid::Battery>());
// .as returns a pointer to a casted DeviceInterface. This pointer must
// not, under any circumstances, be deleted outside Solid!
// https://bugs.kde.org/show_bug.cgi?id=413003
const auto battery = m_batteries.value(index.row()).as<Solid::Battery>();
QQmlEngine::setObjectOwnership(battery, QQmlEngine::CppOwnership);
return QVariant::fromValue(battery);
} else if (role == ProductRole) {
const Solid::Device device = m_batteries.value(index.row());
return device.product();
......
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