I’m developing an extension and periodically get errors related to callbacks and garbage collection:
Attempting to run a JS callback during garbage collection. This is most likely
caused by destroying a Clutter actor or GTK widget with ::destroy signal
connected, or using the destroy(), dispose(), or remove() vfuncs. Because it
would crash the application, it has been blocked.
The offending callback was BindingTransformFunc().
and
Attempting to call back into JSAPI during the sweeping phase of GC. This is most
likely caused by not destroying a Clutter actor or Gtk+ widget with ::destroy
signals connected, but can also be caused by using the destroy(), dispose(), or
remove() vfuncs. Because it would crash the application, it has been blocked and
the JS callback not invoked.
After reading this thread with a similar problem, I came to the conclusion that in my case, the cause of these errors is asynchronous loading of files:
_loadFile(file) {
return new Promise((resolve, reject) => {
file.load_contents_async(null, (...[, result]) => {
try {
const [ok, bytes] = file.load_contents_finish(result);
if (!ok) {
throw new Error(`Uknnown error`);
}
const text = new TextDecoder().decode(bytes);
resolve(text);
} catch (error) {
reject(error);
}
});
});
}
//...
async _loadMenuItemFromFile(file) {
const text = await this._storage.loadFile(file);
if (!text) {
return;
}
const menuItem = this._createMenuItem(text);
this.menu.addMenuItem(menuItem);
}
If I read files not asynchronously, no errors occur, but such code can cause the shell to hang for a while, so I need to do it asynchronously.
Is there any way around this?