The following is a simple scenario for some GNOME extension:
- Enable the extension. The extension is a class which extends
Clutter.Actor
. - It creates an actor called
myActor
and adds it:this.add_child(myActor)
. - Then it calls an asynchronous
time-consuming function
this._tcFunction()
which in the end does something withmyActor
.
Here's where I run into a problem:
We disable (run
this.destroy()
) the extension immediately after enabling it.On disabling,
this.destroy()
runs GObject'sthis.run_dispose()
to collect garbage. However, ifthis._tcFunction()
has not finished running, it'll later try to accessmyActor
which might have been already deallocated bythis.run_dispose()
.
One way to go about this, is to define a boolean variable in this.destroy()
destroy() {
this._destroying = true
// ...
this.run_dispose;
}
and then add a check in this._tcFunction()
, e.g.
async _tcFunction() {
await this._timeConsumingStuff();
if (this._destroying === true) { return; }
myActor.show();
}
My question: is there a nicer way to deal with these situations? Maybe with Gio.Cancellable()
? AFAIK, there's no easy way to stop an async function in javascript...