|
GarbageCollection
JSCocoa Garbage Collection uses JavascriptCore's Garbage Collection engine. You can safely manipulate Objective-C objects without fear of them being deallocated as JSCocoa retains and releases them for you. But you have to be careful when allocating objects yourself and you have to know when they can be deallocated. When are ObjC objects actually released ?ObjC objects are released during JavascriptCore Garbage Collection when no Javascript variables are using them anymore. Getting objects back from the runtime in the global scope (not in a function) Getting objects back from the runtime in a function Allocating objects with instance Allocating objects with alloc/init WARNING ! Forgetting the var declarator in a function will declare a variable in the global scope
Live instance countFor JSCocoa classes only JSCocoa logs instance count. Call JSCocoaController.logInstanceStats Manually start Garbage CollectionCall JSCocoaController.garbageCollect. You need to do that only if you want an object to be released NOW. Javascript name bindingJavascript does not copy objects when evaluating myVar = someObject; myVar2 = someObject — a new name is bound to the object, therefore both variables myVar and myVar2 point to someObject. JavascriptCore's Garbage Collector handles that itself, there's no extra retaining or releasing happening here. JSCocoa boxes ObjC objectsWhen evaluating NSWorkspace.sharedWorkspace.activeApplication, JSCocoa gets called back by Javascript and has to give back Javascript objects to Javascript. In that case, JSCocoa will :
That's how the bridge works : JavascriptCore can only manipulate Javascript objects so JSCocoa boxes ObjC objects in Javascript objects. Each time this happens the boxed object is retained, each time the Javascript object is destroyed the boxed object is released. That's why you can use any object you get from any method call safely as it will be released automatically when Garbage Collection happens. Boxing is done with JSCocoaPrivateObject. More in SampleBridgeEvaluation. |
Sign in to add a comment