signalTracker: Remove SignalTracker after its last signal got untracked

The object the SignalTracker belongs to is stored in a map managed by
the SignalManager which keeps a reference to that object. This map is
never destroyed nor is any entry ever removed. This leads to all objects
that ever had SignalTrackers used on them being kept alive even after
all references outside of the SignalTracker are long gone. This then
also extends to other objects which are leaked indirectly through
reference chains from these objects.

And if some of those objects are GObjects, this will prevent them from
being finalized, leaking further resources. A StWidget for example will
not release its shadow textures.

Fix this by destroying the SignalTracker and removing it from the
SignalManager once the last signal it was tracking has been untracked.

A WeakMap could have been used as well, but we need the Map to be
iterable in some of the following changes.

Closes: https://gitlab.gnome.org/GNOME/gnome-shell/-/issues/5807
Closes: https://gitlab.gnome.org/GNOME/gnome-shell/-/issues/5796
Part-of: <https://gitlab.gnome.org/GNOME/gnome-shell/-/merge_requests/2466>
This commit is contained in:
Sebastian Keller 2022-08-29 00:03:50 +02:00 committed by Marge Bot
parent 54ee728aa0
commit 91ce5ca960

View File

@ -65,6 +65,14 @@ class SignalManager {
maybeGetSignalTracker(obj) {
return this._signalTrackers.get(obj) ?? null;
}
/*
* @param {Object} obj - object to remove signal tracker for
* @returns {void}
*/
removeSignalTracker(obj) {
this._signalTrackers.delete(obj);
}
}
class SignalTracker {
@ -124,6 +132,16 @@ class SignalTracker {
this._disconnectSignalForProto(this._getObjectProto(obj), obj, id);
}
_removeTracker() {
if (this._ownerDestroyId)
this._disconnectSignal(this._owner, this._ownerDestroyId);
SignalManager.getDefault().removeSignalTracker(this._owner);
delete this._ownerDestroyId;
delete this._owner;
}
/**
* @param {Object} obj - tracked object
* @param {...number} handlerIds - tracked handler IDs
@ -149,6 +167,9 @@ class SignalTracker {
this._disconnectSignalForProto(ownerProto, this._owner, id));
if (destroyId)
this._disconnectSignal(obj, destroyId);
if (this._map.size === 0)
this._removeTracker();
}
/**
@ -163,12 +184,7 @@ class SignalTracker {
*/
destroy() {
this.clear();
if (this._ownerDestroyId)
this._disconnectSignal(this._owner, this._ownerDestroyId);
delete this._ownerDestroyId;
delete this._owner;
this._removeTracker();
}
}