I am creating a C++ program that embeds Spidermonkey JS library. The program is running fine but it it is leaking memory in JS_NewRuntime call with more than one thread. It is leaking 32 bytes per thread.
Spidermonkey JS version : 45
NSPR version: 4.15
Sample program attached(sample.cpp):
// following code might be needed in some case
#define __STDC_LIMIT_MACROS
#include <stdint.h>
#include "nspr/prthread.h"
#include "nspr/prinit.h"
#include "jsapi.h"
#include "js/Initialization.h"
/* The class of the global object. */
static JSClass global_class = {
"global",
JSCLASS_GLOBAL_FLAGS,
nullptr,
nullptr,
nullptr,
nullptr,
nullptr,
nullptr,
nullptr,
nullptr,
nullptr,
nullptr,
nullptr,
JS_GlobalObjectTraceHook
};
static void CreateThreads(void *);
int main(int argc, const char *argv[])
{
JS_Init();
PRThread* Thread[5];
for(int i=0; i<5; i++)
Thread[i] = PR_CreateThread(PR_USER_THREAD, &CreateThreads, NULL,PR_PRIORITY_NORMAL, PR_GLOBAL_THREAD, PR_JOINABLE_THREAD, 0);
for(int i=0; i<5; i++)
PR_JoinThread(Thread[i]);
JS_ShutDown();
return 0;
}
void CreateThreads(void *arg)
{
JSRuntime *rt = JS_NewRuntime(8L * 1024 * 1024);
if (!rt)
return ;
JS_DestroyRuntime(rt);
}
Command to compile: g++ --std=c++11 -DDEBUG -o a.out sample.cpp -I./include -Llib -lmozjs-45 -lpthread -lnspr4
Command to execute: LD_LIBRARY_PATH=lib/ valgrind --leak-check=full --log-file=./leak.log ./a.out
Valgrind Stacktrace:
128 bytes in 4 blocks are definitely lost in loss record 39 of 59
==13653== at 0x4C27BE3: malloc (vg_replace_malloc.c:299)
==13653== by 0x5753B86: js_malloc(unsigned long) (Utility.h:221)
==13653== by 0x5757FF3: _Z6js_newIN4nspr6ThreadEIDnS2_bEEPT_DpOT0_ (Utility.h:333)
==13653== by 0x575578F: PR_GetCurrentThread() (PosixNSPR.cpp:146)
==13653== by 0x575F6ED: JSRuntime::init(unsigned int, unsigned int) (Runtime.cpp:265)
==13653== by 0x54BE53C: JS_NewRuntime(unsigned int, unsigned int, JSRuntime*) (jsapi.cpp:469)
==13653== by 0x4009EB: CreateThreads(void*) (in /vagrant/sandhya_workspae/t/jssample_new/a.out)
==13653== by 0x6CCD5A9: _pt_root (ptthread.c:216)
==13653== by 0x6A82DC4: start_thread (in /usr/lib64/
libpthread-2.17.so)
==13653== by 0x77FF73C: clone (in /usr/lib64/
libc-2.17.so)