Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/lua/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/56.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C luajit分段故障-不在lua-5.2中_C_Lua_Luajit - Fatal编程技术网

C luajit分段故障-不在lua-5.2中

C luajit分段故障-不在lua-5.2中,c,lua,luajit,C,Lua,Luajit,你好,我正在尝试切换到luajit。 下面的代码在使用liblua5.2时编译并运行良好。 当试图编译和链接luajit-2.0时,它编译得很好,但有错误 有人给我一个提示吗 汇编5.2: gcc -g -O0 -I/usr/include/lua5.2/ -llua5.2 -o lua_sample lua_sample.c 编译luajit-2.0 gcc -Wall -I/usr/local/include/luajit-2.0/ -lluajit-5.1 -o luajit_samp

你好,我正在尝试切换到luajit。 下面的代码在使用liblua5.2时编译并运行良好。 当试图编译和链接luajit-2.0时,它编译得很好,但有错误

有人给我一个提示吗

汇编5.2:

gcc -g -O0 -I/usr/include/lua5.2/ -llua5.2 -o lua_sample lua_sample.c
编译luajit-2.0

 gcc -Wall -I/usr/local/include/luajit-2.0/ -lluajit-5.1 -o luajit_sample lua_sample.c
5.2的输出:

# ./lua_sample 
LUA: MAIN
Script ended with 22
LUA: ######## HOOK CALLED - Start
LUA: # service_id: 322
LUA: # service_name: sssasdf
LUA: #     setting new state to 4
SET_STATUS: Service Object: sssasdf
SET_STATUS: Code: 4
LUA: #    call returned RES:-123
LUA: ######## HOOK CALLED - END
HOOK ended with -123
luajit上的输出:

# ./luajit_sample 
LUA: MAIN
Segmentation fault
lua_sample.c

#include <stdio.h>

#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"

/* the Lua interpreter */

struct service {
    int service_id;
    char service_name[50];
};

static int lua_print(lua_State *L) {
    int i;
    int nargs = lua_gettop(L);

     for (i=1; i <= nargs; ++i) {
        printf("LUA: %s\n",  lua_tostring(L, i));
    }   

}
static int lua_callback_service_set_status(lua_State *L) {
    int status;
    struct service * svc;


    svc=lua_touserdata(L, 1);
    status=lua_tonumber(L, 2);

    printf("SET_STATUS: Service Object: %s\n", svc->service_name);
    printf("SET_STATUS: Code: %d\n",status);

    lua_pushnumber(L, -123);
    return 1;
}

int main ( int argc, char *argv[] )
{
    int res;
    lua_State* L;


    struct service svc = {
        .service_id=322,
        .service_name="sssasdf"
    };

    /* initialize Lua */    
    L = luaL_newstate();

    /* load various Lua libraries */
    luaL_openlibs(L);

    lua_register(L, "callback_service_set_status", lua_callback_service_set_status);
    lua_register(L, "print", lua_print);



    /* run the script */
    luaL_dostring(L, "return dofile('sample.lua')");


    res = lua_tonumber(L, -1);
    printf("Script ended with %d\n", res);


    /* the function name */
    lua_getglobal(L, "callback_service_finish_hook");


    lua_pushlightuserdata(L, (void*)&svc );


    lua_newtable(L);

    lua_pushliteral(L, "service_id" );
    lua_pushnumber(L, svc.service_id );
    lua_settable(L, -3);  


    lua_pushliteral(L, "service_name" );
    lua_pushstring(L, svc.service_name );
    lua_settable(L, -3);  





    if(lua_pcall(L, 2, 1, 0) != 0 ) {

        printf("error running function `callback_service_finish_hook': %s\n", lua_tostring(L, -1));

    } else {
        /* get the result */    
        res = (int)lua_tonumber(L, -1);
        lua_pop(L, 1);
        printf("HOOK ended with %d\n", res);
    }


    /* print the result */






    /* cleanup Lua */
    lua_close(L);

    return 0;
}

返回0lua\u print()
中的code>。 添加此选项-修复了segfault

thx-对于所有的评论者-添加
-Wall-Werror-pedantic
将表明
lua_print()
不会返回值

所以最后的lua_打印看起来像:

static int lua_print(lua_State *L) {
    int i;
    int nargs = lua_gettop(L);

    for (i=1; i <= nargs; ++i) {
        printf("LUA: %s\n",  lua_tostring(L, i));
    }   
   return 0;
}
static int lua\u print(lua\u State*L){
int i;
int nargs=lua_gettop(L);

对于(i=1;i要缩小崩溃发生的范围,请首先构建程序的调试版本(在构建时添加
-g
标志),然后在调试器中运行它。调试器将在崩溃位置停止,让您检查函数调用堆栈。查看调用堆栈,如果崩溃不在代码中,则沿着调用堆栈一直走到代码中。在那里,您可以检查变量的值,以查看它们是否与您期望的值相同。如果没有,则g否则,请编辑您的问题以显示崩溃发生的位置(在您的代码中)。还请注意,这实际上可能是Lua/Luajit中的一个错误。尝试最小化代码以复制问题,并确保您的代码正常工作(一个好的开始是添加更多编译器警告,例如
-Wall-Wextra-pedantic
)。如果崩溃仍然发生,并且您的代码尽可能好,那么请转到Lua并添加一个错误报告,显示您拥有的代码。您没有检查来自
Lua\u-touserdata()的返回值
可以返回
NULL
,但您很快就会取消引用。
lua\u print
没有返回类型。请尝试
返回0;
,注意
-Wall-pedantic
会警告您这一点。提供您自己的答案并接受它。
static int lua_print(lua_State *L) {
    int i;
    int nargs = lua_gettop(L);

    for (i=1; i <= nargs; ++i) {
        printf("LUA: %s\n",  lua_tostring(L, i));
    }   
   return 0;
}