Skip to content

object: Ignore toggle notifications after disposal

Marco Trevisan requested to merge 3v1n0/gjs:always-unref-wrappee into master

As per commit d37d6604 we are not removing a toggle reference on disposed objects, however it may happen that a disposed object (but not yet finalized) is still using the toggle references while we're releasing it, and in such case we must always remove the toggle ref, otherwise GObject (that doesn't remove toggle notifications on disposition) will notify us after that the wrapper has been finalized, causing a crash because at that point the the GObject is still very well alive (and so its qdata) and so when we'll try to get the gjs private data from it, we'll end up accessing dead memory.

So, on weak notify callback the object (that we get on disposition so the object is memory is still valid, and so its toggle notifications) remove the toggle reference that we have and consequently toggle down the JSObject wrapper, unrooting it so that the garbage collector can pick it (this may also make it a bit more reactive, without waiting for the last reference being removed if disposition happens as consequence of a run_dispose() call).

We keep the m_uses_toggle_ref set though to avoid we add another one, but at this point we also have to check whether the object is also disposed before considering the toggle ref active, and per this in such case we've to only release the m_ptr when releasing the native object not to access to potentially finalized data.

Fixes: #387 (closed)

Edited by Philip Chimento

Merge request reports