[webkit-gtk] JavaScriptCore GObject Bindings - Memory Management

tevaum at gmail.com tevaum at gmail.com
Fri Mar 10 04:49:03 PST 2017

Hi. I started implementing a JavaScriptCore GObject binding in [1]. This
API provides two main objects: JSCContext, representing JSGlobalContextRef,
and JSCValue, representing JSValueRef. It's a first patch and does't do any
memory management, because I wasn't really aware of how JSC handles
JSValueRefs and stuff.

Asking on JSC list they replied that JSValues are garbage collected and
when you need to keep a reference to it in the heap you should protect
them. [2] This is already done by the bindings: JSValueRefs are protected
on contruction of JSCValue and unprotected on destruction, but JSCValues
were implemented as initially owned and noone's keeping their reference to
unref them when needed.

So in the current state, we are leaking JSCValues and, as JSCValues protect
JSValueRefs, memory is never freed.

JavaScriptCore doesn't have any API to directly free JSValueRefs, so
JSCValues needs to be destructed to unprotect JSValueRefs.

My thought is to Adopt JSCValues when they are created using a private API
in the JSCContext (jscContextAdoptJSCValue(JSCValue*) ?). As JSCValues have
a JSCContext reference, we can call jscContextAdoptJSCValue in JSCValue's
constructed callback. With this private API, JSCContext will get a JSCValue
reference and add it to a HashMap that will map the JSValueRef to the
JSCValue. When added to the HashMap, JSCValue will be owned by the context
and will be freed when the context is unrefed.

What do you think about this idea?

[1] *https://bugs.webkit.org/show_bug.cgi?id=164061
[2] https://lists.webkit.org/pipermail/jsc-dev/2016-November/000084.html
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.webkit.org/pipermail/webkit-gtk/attachments/20170310/b06d3ea6/attachment.html>

More information about the webkit-gtk mailing list