Free JS Class Proto carefully #845
-
When I create a JS class, I have to create a new proto as an object, set constructor function, call JS_SetConstructor and use JS_SetClassProto. However, the thing is, when we set the class proto, quickjs does not allocate the memory for it at all but until the class is called quickjs actually allocated a memory on the heap to store the proto. Whenever a constructor is called the reference count of the proto is increased. When the program ends, I have to call JS_FreeValue, but how could I know how many time the proto has been referenced so that I can call that amount of JS_FreeValue so that the assert of the p->ref_count > 0 doesn't scream. Also, thanks for beautiful project it helped a lot! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
When you call JS_SetClassProto, quickjs takes ownership of the object and ensures it's freed when the JSContext is destroyed. In other words, you should normally no longer touch it, but if you do, call JS_DupValue before JS_SetClassProto. JS_SetConstructor does not take ownership, so business as usual. |
Beta Was this translation helpful? Give feedback.
When you call JS_SetClassProto, quickjs takes ownership of the object and ensures it's freed when the JSContext is destroyed. In other words, you should normally no longer touch it, but if you do, call JS_DupValue before JS_SetClassProto.
JS_SetConstructor does not take ownership, so business as usual.