luaJit 如何释放从 C 函数返回的 char* 数据

这是我想要调用的 C 函数:

char* decrypt(const char* str, int len) {
    char* endata = malloc(len/2);
    //赋值
    return endata;
}

下面是 Lua 内容:

local ffi = require "ffi"
local rsa = ffi.load("test.so")

ffi.cdef[[
char* decrypt(const char* str, int len);
]]

local str = "256b5740ed16e5b15146816f77b0ab74de43d58ab367e9ac850642514f5cc4880e899a6a7775aa"
local c_str = test.decrypt(str, 256)
local lua_str = ffi.string(c_str, 128)

luajit 会自动释放 c_str 吗?

点赞
用户2060502
用户2060502

LuaJIT 会自动释放 c_str 吗?

不会。 LuaJIT 不知道这个指针的用途。

2017-03-21 21:14:51