[webkit-help] Memory management and the C API

Alexey Proskuryakov ap at webkit.org
Thu Apr 1 09:07:20 PDT 2010


01.04.2010, в 06:04, Allison Newman написал(а):

> I'm having a little trouble getting some good information on the C API used for generating JavaScript bindings, specifically memory-management and the interaction of JavaScript objects generated in C code and the garbage collector.

The first place to go for such information is JavaScriptCore headers. For "create" functions like JSStringCreateWithUTF8CString(), comments say "Ownership follows the Create Rule," and the rule itself is explained e.g. at <http://developer.apple.com/mac/library/documentation/corefoundation/Conceptual/CFMemoryMgmt/Concepts/Ownership.html>.

> For example, when I call JSStringCreateWithUTF8CString(), do I need to do a JSStringRetain() on the resulting string object?
> And if not, does that mean that I have to do an explicit JSStringRelease()? Are these objects subject to garbage collection at all, or do they need to be explicitly retained/released? Or does the act of doing a retain disable garbage collection on an object? 


JSStrings are not JavaScript objects, and thus aren't subject to garbage collection. The result of a "create" function needs to be released.

JavaScript objects and values as created by JSObjectMake and JSValueMakeString are different (which is why there is no "create" in these functions' names). They are subject to GC, and there are no retain/release functions for these, but there are JSValueProtect() and JSValueUnrotect(). Again, there is documentation for these functions in headers.

> If they are subject to garbage collection, when does the garbage collection run? Is it in a separate thread? 

This is an implementation detail that can change.

- WBR, Alexey Proskuryakov


More information about the webkit-help mailing list