0

This afternoon I recompiled the server in CentOS7, and then when I started CentOS7, I saw that the process had been suspended. lua5.1 The Static library was compiled in CentOS6. The gcc version of CentOS6 is 4.4.7, and the gcc version of CentOS7 is 4.8.5. Then I debugged using gdb, and gdb show:

Breakpoint 4, SGPlatformActMgr::InitDBFromConf (L=0x7fffe577901c) at gamelogic/bg/SGPlatformActMgr.cpp:454
454     ERROR_LOG("astActInfo count:%d InitActDB begin\n" , astActInfo.GetCount());
(gdb) l
449         astActInfo.Add(stActInfo);
450 
451         lua_pop(L, 1);
452     }
453 
454     ERROR_LOG("astActInfo count:%d InitActDB begin\n" , astActInfo.GetCount());
455     iRet = InitActDB(L, astActInfo);
456     if (iRet)
457     {
458         ERROR_LOG("InitActDB failed:%d\n", iRet);
(gdb) n
455     iRet = InitActDB(L, astActInfo);
(gdb) p L
$2 = (lua_State *) 0x7fffe577901c
(gdb) s

Program received signal SIGSEGV, Segmentation fault.
0x000000000056b0fa in InitActDB (L=<error reading variable: Cannot access memory at address 0x7fffff5e4a48>, 
    astActInfo=<error reading variable: Cannot access memory at address 0x7fffff5e4a40>)
    at gamelogic/bg/SGPlatformActMgr.cpp:292
292 {
(gdb) print L
Cannot access memory at address 0x7fffff5e4a48

Below is the possibly problematic code, InitDBFromConf will be called in lua like this InitDBFromConf(lua_act_config):

int SGPlatformActMgr::InitDBFromConf(lua_State* L)
{
    //...
    //do something
    SGArray<TPlatformActInfo, 150> astActInfo;
    int iTableIdx = 1;
    int iRet = 0;
    lua_pushnil(L);
    while (lua_next(L, iTableIdx) != 0)
    {
        TPlatformActInfo stActInfo;
        iRet = TransformActInfo(L, stActInfo);
        if (iRet)
        {
            ERROR_LOG("invalid TransformActInfo failed(%d)\n", iRet);
            return 0;
        }
        astActInfo.Add(stActInfo);
        lua_pop(L, 1);
    }
    
    iRet = InitActDB(L, astActInfo);
    //do something
    //....
}

in function TransformActInfo:

#define ASSIGN_LUA_VALUE(L,VAR,KEY,RET_CODE)\
do \
{   \
    lua_pushstring((L), (KEY)); \
    lua_rawget((L), -2);    \
if (!lua_isnumber((L), -1)) \
{   \
    ERROR_LOG("expect number but got:%s\n", lua_typename((L), lua_type(L, -1)));    \
    return RET_CODE;    \
}   \
    (VAR) = lua_tonumber(L, -1); \
    lua_pop((L), 1);    \
} while (0)

#define ASSIGN_LUA_STR_VALUE(L,VAR,KEY,RET_CODE)\
do \
{   \
    lua_pushstring((L), (KEY)); \
    lua_rawget((L), -2);    \
if (!lua_isstring((L), -1)) \
{   \
    ERROR_LOG("expect string but got:%s\n", lua_typename((L), lua_type(L, -1)));    \
    return RET_CODE;    \
}   \
    const char* pTempStr = lua_tostring(L, -1); \
    SAFE_COPYSTR_TO_CARRAY((VAR), pTempStr); \
    lua_pop((L), 1);    \
} while (0)
int TransformActInfo(lua_State* L, TPlatformActInfo& stActInfo)
{
    if (!lua_istable(L, -1))
    {
        ERROR_LOG("expect a table but got:%s\n", lua_typename(L, lua_type(L, -1)));
        return -1;
    }

    ASSIGN_LUA_VALUE(L, stActInfo.m_uiActID, "id", -100);
        //.....
    ASSIGN_LUA_STR_VALUE(L, stActInfo.m_acExtStr, "ext_str", -112);

    return 0;
}

I have tried the following methods to solve or debug the problem:

  • I used the gdb watch command to monitor the value of the L parameter and break the program when it changed. I found that between the SGPlatformActMgr::InitDBFromConf function and the InitActDB function, there was no code or operation that caused the value of the L parameter to change or be released.

  • I used the gdb break command to set a breakpoint and print the value of the L parameter before call function InitActDB. I found that it was a valid lua_State pointer. pointer.

  • I used the gdb step command to execute the code in the InitActDB function step by step and found that a segmentation fault occurred on the first line of the function.

  • I could call the InitActDB function normally and pass in valid L and astActInfo parameters.

  • I could use the L and astActInfo parameters normally in the InitActDB function without segmentation faults or other exceptions.

  • I could find and solve the cause and solution of the segmentation fault. thank you!

1 Answer 1

0

Sorry, I found out that it is because SGArray<TPlatformActInfo, 150> astActInfo; used too much stack space, resulting in stack overflow. After my calculation, the size of a TPlatformActInfo is about 40kb, and 150 are about 6MB, resulting in a stack overflow. But I'm confused why it works fine on CentOS.

1
  • Edit your question for adding this addition information, don't use answer block for additions.
    – 273K
    Jun 10 at 19:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.