Is JS_malloc only used to allocate memory for javascript types in Spidermonkey?
If I need to allocate memory for a third-party datatype (not a JSObject or similar), is it ok to use malloc?
What are the differences between JS_malloc and C malloc?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
JS_mallocis just there because it is guaranteed to use the same allocator as the Spidermonkey itself does, which may not be the same allocator asmallocin your code. In particular some popular OSes (e.g. Windows) support separate heaps with separate allocators per shared library; if you’re dynamically linking to Spidermonkey then calling Spidermonkey’sfreeon memory youmallocin your code would crash.So if you’re going to deallocate the object yourself, you can use either
malloc/freeorJS_malloc/JS_freeas long as you’re consistent. It doesn’t matter much which one you use, unless you have specific requirements on which DLL’s heap you want it to live in (e.g. you plan to unload the Spidermonkey DLL at some point while some of these objects are live).If you’re doing the allocation but expect Spidermonkey to do the deallocation, you need to use
JS_malloc.