kbranigan/cJSON

Possible memory leak

Closed this issue · 8 comments

This is discussed here:
bungle/lua-resty-libcjson#6

That is LuaJIT FFI binding to this library. There is also an example that triggers this problem. I'm pretty sure the leak is in C-module, but it could be in my code as well, but I have not spotted it.

I'm not familiar with Lua or LuaJIT but any memory leaks I've ever encountered with this library have been issues in my code with not properly freeing the alloc'd memory (i.e. calling cJSON_Delete). Perhaps provide a snippet of what .encode looks like in your implementation?

So when does the garbage collection happen? Again, not familiar with the platform but if the loop is blocking and GC doesn't happen, one would expect the usage to increase until GC.

I think it leaks even with single call to that encode. And GC doesn't free it, that's why I suspect the problem is with C code, but not sure.

I have also tried manually calling cJSON_Deletes, but that leaks also.

Interesting. Have you tried printing out pointers of the objects created (if that's possible)? That way you can at least be sure it's trying to delete the right thing.

Yes, let's see where the problem is, possible places:

  1. bungle/lua-resty-libcjson#6
  2. LuaJIT/LuaJIT#163
  3. #33

I tried even more tests on my bindings, and whatever I do, I see luajit taking more and more memory while it is running.

@someburner, Yes, the error was in 1. (aka my code) after all. Thanks for helping and looking at it.