Hello agentzh,
First thanks for being so generous with your time, it's amazing work! Please be sure that there is no expectation of hand-holding, I wrote the code example from scratch to illustrate an issue we are facing. Was trying to be very specific with a complete example.
With that said, as you pointed out, our example was not correct - something was missing - but that wasn't the problem.
An update example - even more minimal - is below. We identified that the issue does *not* occur on MAC OS, but does on Ubuntu - with the exact same code. Perhaps it is actually not due to the OS but to the compiler?
For Mac OS it is: Apple LLVM version 7.0.2 (clang-700.1.81)
And for Ubuntu: gcc version 5.3.0 (GCC)
c code:
#include <stdlib.h>
int** allocate_data(int size) {
int** p = (int**)malloc(sizeof(int*)*size);
for (int i = 0; i < size; i++) {
p[i] = (int*)malloc(sizeof(int));
}
return p;
}
void free_data(int** p, int size) {
for (int i = 0; i < size; i++) {
free(p[i]);
}
free(p);
}
lua module:
ffi = require("ffi")
ffi.cdef[[
int** allocate_data(int size);
void free_data(int** p, int size);
]]
function lib_allocate(a)
local module = ffi.load("Allocate",false)
local p = module.allocate_data(a);
module.free_data(p,a);
end
nginx.conf lua blocks:
init_by_lua_block {
local ffi_allocate = require "ffi_allocate";
}
location /allocate {
content_by_lua_block {
local ffi_allocate = require "ffi_allocate";
lib_allocate(50000000)
ngx.say("{}")
}
}
On linux, memory is not released, whereas this issue doesn't occur on Mac OS.
If you have any pointer as to what to try next, would be greatly appreciated.
- DK
On Wednesday, March 16, 2016 at 12:26:20 PM UTC-7, agentzh wrote:
Hello!
On Sun, Mar 13, 2016 at 5:53 PM, kaspersky_us wrote:
> I have a memory problem integrating C++ code into Openresty through FFI. I
> isolated the issue to the use of double pointers. Lua calls the following
> function (wrapped in C) with amount = 10000000:
>
It seems that you allocate a CAllocate object in your Lua code but
there is nothing who is responsible for freeing it. Thus an obvious
memory leak.
FFI programming is essentially like C programming. No hand-holding
here. And I don't think it's interesting for us to debug issues in
your own code for you :)
Regards,
-agentzh