문제

Function returns successfully and I can use values from the table but the error "Debug Assertion Failed" shows up and it's the end. I know that the problem with assert is in the for loop but don't exactly know how to fix that. Thanks in advance.

static int l_xmlNodeGetValues(lua_State *L)
{
  int iDocID = luaL_checkint(L, 1);
  const char *pszNodeName = luaL_checkstring(L, 2);

  CConfig *file = docs.at(iDocID);
  int i = 1;
  lua_newtable(L);
  for( TiXmlElement *e = file->GetRootElement()->FirstChildElement(pszNodeName);
       e; e = e->NextSiblingElement(pszNodeName) )
  {
      lua_pushstring(L, e->GetText());
      lua_rawseti(L,-2,i);
      i++;
  }
  return 1;
}

EDIT: When I set int i; on 0 it works but forgets about last element. Why it doesn't if i == 1 ?

The assertion failed shows up when lua_rawseti(L,-2,i); and i == 1

Since there is no solution which solves my problem I'll try to describe what it does and what's the output in these 2 cases. I simply want to get all values from specified node in xml file:

<root>
    <node>A</node>
    <node>B</node>
    <node>C</node>
    <node>D</node>
</root>

The script looks like that:

xmlfile = xmlOpenFile( "myfile.xml", "root" );
if ( xmlfile ) then
    for _, v in ipairs( xmlNodeGetValues( xmlfile, "node" ) ) do
        print( v );
    end
end

PROBLEM:

int i = 1;

output:

A B C D !!!debug assertion failed!!!

------------------------------------------------------

int i = 0;

output:

B C D no errors...

도움이 되었습니까?

해결책

Are you sure there's no error in your code?

I just checked this solution and it seems to work, the code prints the table it just created:

#include <lua.hpp>
#include <stdio.h>

static int fun(lua_State * L)
{
    int i;
    lua_newtable(L);
    for(i = 0; i < 10; i++ )
    {
        lua_pushstring(L, "A");
        lua_rawseti(L,-2,i);
    }

    lua_setglobal(L, "t");
    return 1;
}

int main()
{
    lua_State* L = luaL_newstate();
    luaL_openlibs(L);

    fun(L);

    if (luaL_dostring(L, "for k,v in ipairs(t) do print(k,v); end;\n"))
    printf("%s\n",luaL_checkstring(L, -1));

    lua_close(L);
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top