LuaJIT FFI:程序终止时的分段错误

时间:2017-01-24 11:28:21

标签: c lua ffi luajit

我正在尝试使用LuaJIT ffi做一件非常简单的事情。尝试用C代码打开文件然后关闭文件。

以下是C代码fileWrite.c

#include<stdint.h>
#include<stdio.h>
#include<string.h>

FILE * createFile(char *fname){
    strcat(fname,".bin");
    FILE *file = fopen(fname, "wb");
    return file;
}

void close(FILE *fp){
    int status = fclose(fp);
}

ffi在lua中调用如下

local ffi = require("ffi") 
local so_file_path_name = "./fileWrite.so" --path for .so file
local qCLib  = ffi.load(so_file_path_name) 

local binfilepath = "./"

ffi.cdef[[
  void *malloc(size_t size);
  void free(void *ptr);

  typedef struct FILE FILE;  
  FILE* createFile(char *fname);
  void close(FILE *fp);
  ]]

function create(fileName) 
  filepath = binfilepath..fileName
  local filePathArg = ffi.new("char [?]", #filepath, filepath)
  local fp = qCLib["createFile"](filePathArg);
  return fp;
end

function close(fp) 
    qCLib["close"](fp)
    fp = nil
end

file = create("test")
print(file)
close(file)
print("after closing") 

但是当我运行代码时,我收到了分段错误错误:

$ luajit test.lua 
cdata<struct FILE *>: 0x01dc9610
after closing
Segmentation fault (core dumped)

我在ubuntu 14.04上使用luajit版LuaJIT 2.0.4

有关如何调试此分段错误的任何指示都会有所帮助

0 个答案:

没有答案