首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >luajit分段断层-不在lua-5.2中

luajit分段断层-不在lua-5.2中
EN

Stack Overflow用户
提问于 2015-01-29 09:15:35
回答 1查看 1K关注 0票数 1

你好,我正试着转到卢杰特。下面的代码在使用libla5.2时编译并运行良好。当尝试编译并链接到luajit-2.0时,它会编译精细但分段的错误

有人给我个提示吗?

汇编5.2:

代码语言:javascript
复制
gcc -g -O0 -I/usr/include/lua5.2/ -llua5.2 -o lua_sample lua_sample.c

编译luajit-2.0

代码语言:javascript
复制
 gcc -Wall -I/usr/local/include/luajit-2.0/ -lluajit-5.1 -o luajit_sample lua_sample.c

5.2的产出:

代码语言:javascript
复制
# ./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的产出:

代码语言:javascript
复制
# ./luajit_sample 
LUA: MAIN
Segmentation fault

lua_sample.c

代码语言:javascript
复制
#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;
}

sample.lua (在同一个文件夹中)

代码语言:javascript
复制
function callback_service_finish_hook(svc_obj, svc_table) 
    print("######## HOOK CALLED - Start")
    print("# service_id: " ..  svc_table["service_id"])
    print("# service_name: " ..  svc_table["service_name"])
    print("#       setting new state to 4")
    r = callback_service_set_status(svc_obj, 4)
    print("#    call returned RES:" .. r)
    print("######## HOOK CALLED - END")
    return r
end


print("MAIN")
return 22
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-01-29 19:19:35

return 0; in lua_print()失踪了。加上这个-修复了分段故障。

thx -对所有批判者来说-添加-Wall -Werror -pedantic将显示lua_print()不返回值。

因此,最终的lua_print看起来是:

代码语言:javascript
复制
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;
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28210855

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档