Commit 7b330751 authored by Philip Chimento's avatar Philip Chimento

boxed: Mark pointer as unowned when discarding boxed object

Sometimes, in the constructor of a boxed object, we will delegate to
another constructor, such as GLib.Error() or GLib.Variant.new_internal()
and replace the object under construction by the object returned by
the delegated constructor.

When that happens, the internal GBoxed pointer of the BoxedInstance
remains null. That causes problems when eventually the discarded boxed
object is garbage collected, and we try to free the pointer. In those
cases, mark it as unowned so it is not freed.
parent cf71e3ba
......@@ -304,7 +304,13 @@ bool BoxedInstance::constructor_impl(JSContext* context, JS::HandleObject obj,
if (!boxed_invoke_constructor(context, obj, atoms.new_internal(), args))
return false;
debug_lifecycle("Boxed construction delegated to GVariant constructor");
// The return value of GLib.Variant.new_internal() gets its own
// BoxedInstance, and the one we're setting up in this constructor is
// discarded.
m_not_owning_ptr = true;
debug_lifecycle(
"Boxed construction delegated to GVariant constructor, "
"boxed object discarded");
return true;
}
......@@ -354,7 +360,13 @@ bool BoxedInstance::constructor_impl(JSContext* context, JS::HandleObject obj,
return false;
}
debug_lifecycle("Boxed construction delegated to JS constructor");
// The return value of the JS constructor gets its own BoxedInstance,
// and this one is discarded. Mark that the boxed pointer doesn't need
// to be freed, since it remains null.
m_not_owning_ptr = true;
debug_lifecycle(
"Boxed construction delegated to JS constructor, "
"boxed object discarded");
return true;
} else {
......
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