lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Hi,
I am trying to return a array to Lua from the C function as below:

static int Test(lua_State* L)
{
    lua_newtable(L);//create table
    lua_pushnumber(L,1);//push the value
    lua_rawseti(L,-2,1);//set t[1]=v
    lua_pushnumber(L,2);    
    lua_rawseti(L,-2,2);
    lua_pushnumber(L,3);    
    lua_rawseti(L,-2,3);
    return 1;
}

The Lua code is:

data = testext.Test() -- the testext is DLL which include the Test function
for i=1,table.getn(data),1 do
   print(data [i]) 
end

All the three elements are printed correctly but a _CrtIsValidHeapPointer error appears.
I check the FAQ in the www.lua.org, I think there is no stack overflow problem, 
because the stack size is only 2 or 1. 

A very funny thing is that, if I add a element whose index is 0, everything seems ok:

static int Test(lua_State* L)
{     
    lua_newtable(L);//create table
    lua_pushnumber(L,-1);//push any value
    lua_rawseti(L,-2,0);//set t[0]=v
    lua_pushnumber(L,1);//push the value
    lua_rawseti(L,-2,1);//set t[1]=v
    lua_pushnumber(L,2);
    lua_rawseti(L,-2,2);
    lua_pushnumber(L,3);
    lua_rawseti(L,-2,3);
    return 1;
}

The _CrtIsValidHeapPointer error disappear,
but in the Lua code, table.getn(data) return 3, instead of 4. 

With data[0], the element can still be accessed.

Could you please tell me why?
Thank you.

Regards,
Jason