<div dir="ltr">Hi Geoffrey,<div><br></div><div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">> Therefore, if I'm not immediately returning the created object, it might get cleaned up prematurely.<br>
</blockquote><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><span style="font-family:arial,sans-serif;font-size:12.666666984558105px">Because the GC scans the stack, and the value you just created is on the stack, it won’t be garbage collected right away.</span></blockquote>
<div><br></div><div>What about the case when the creation of an object originates from C++ land? For example, if I tap on a view and it needs to invoke a JS callback. I'm creating an object to pass to JSEvaluateScript or as an argument to JSObjectCallAsFunction.</div>
<div><br></div><div>At this point, the code is not inside the JS stack, so is it possible for an object to be collected between the calls to JSObjectMake and JSObjectCallAsFunction?</div>
<div><br></div></div><div>Thanks!</div></div><div class="gmail_extra"><br><br><div class="gmail_quote">On Sat, Dec 7, 2013 at 8:55 PM, Geoffrey Garen <span dir="ltr"><<a href="mailto:ggaren@apple.com" target="_blank">ggaren@apple.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="im">> My question in short: is it necessary to call JSValueProtect right after object/value creation in C++?<br>
<br>
</div>No.<br>
<div class="im"><br>
> 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.<br>
<br>
</div>Yes.<br>
<div class="im"><br>
> Therefore, if I'm not immediately returning the created object, it might get cleaned up prematurely.<br>
<br>
</div>Because the GC scans the stack, and the value you just created is on the stack, it won’t be garbage collected right away.<br>
<div class="im"><br>
> Could you share some advice on the proper usage of JSValueProtect and on keeping objects from going away unexpectedly?<br>
<br>
</div>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.<br>
<br>
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.<br>
<br>
Geoff</blockquote></div><br><br clear="all"><div><br></div>-- <br>Best regards<br>Alexei Sholik
</div>