ggaren at apple.com
Sat Dec 7 10:55:07 PST 2013
> My question in short: is it necessary to call JSValueProtect right after object/value creation in C++?
> I don't know the semantics of the GC, but I'm assuming that almost any call to a JSC function that takes a context may run a garbage collection cycle.
> Therefore, if I'm not immediately returning the created object, it might get cleaned up prematurely.
Because the GC scans the stack, and the value you just created is on the stack, it won’t be garbage collected right away.
> Could you share some advice on the proper usage of JSValueProtect and on keeping objects from going away unexpectedly?
If you store a JSValueRef or JSObjectRef into a C/C++ heap object, then you should JSValueProtect the JSValueRef/JSObjectRef, since your reference to it will outlast the pointer to it on the stack.
Use JSValueProtect/JSValueUnprotect just like you would use any other reference-counting API — such as CFRetain/CFRelease, NSObject retain/release, or IUnknown AddRef/RemoveRef — with the added feature that you do not need to retain local variables.
More information about the webkit-dev