Subj : How does object persistance work over multiple contexts? To : netscape.public.mozilla.jseng From : eboli Date : Wed Nov 17 2004 07:34 pm Hello, I am trying to embed Spidermonkey into a c++ application I am working on and would like to know if somebody could explain how the contexts work as far as object persistence is concerned. I think I am misunderstanding this, but ultimately what I am trying to achieve is a wrapper class which would load assorted scripts in a library style fashion. Previously I have been creating and using objects on a single context with all initialisation etc as per (http://sourceforge.net/docman/display_doc.php?docid=10853&group_id=50913). That works fine for the moment. As I understand it (and it works through implementation) after a context is created other context's subsequently have access to that object. Now what I figured would happen (figured incorrectly) is that the lifetime for that object would be limited to the context that created it (and/or while it was being referenced by another object). So what I had tried was: Create a new Runtime; Create a new Context1; Create a new Context2; Initialise an object on Context1; Call one of its methods from context 2; - This worked fine; So I figured if I deleted Context1 at this point the reference to that object would be lost; and it would be garbage collected; thus the subsequent call to that objects method on Context2 would fail. Calling JS_DestroyContext(Context1); caused my application to crash, I thought this was probably because as it contained the global object; and the standard classes were also initialised to it, it was probably just a bad thing to do :) So I created a third context; and after destroying the second context the object still persisted. So what I am doing is below; Create a runtime; Create a global context; Create a nonglobalcontext1; Create a nonglobalcontext2; Create an object on nonglobalcontext1; function someObject() { this.someValue = 22; this.someMethod = function() { return this.someValue; }; } var someObjectInstance = new someObject(); Call JS_DestroyContext( nonglobalcontext1 ); Execute a method from someObjectInstance from the nonglobalcontext2; var test = someObjectInstance.someMethod(); and rather then returning undefined or JS_FALSE in an error; I am still getting the return value of the function as if the object is persisting; additionally I have tried calling JS_GC(nonglobalcontext1) before destroying it to no avail. Finally even when I create the object definition / prototype on the first nonglobalcontext; then destroy that context; I can still create an instance of that object; and call its methods from another context (again w/wo JS_GC called) So my question would be in two parts :) … what am I doing wrong; and / or am I even on the correct track here? Thanks for your time Joesph .