1

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...

greatwolf
  • 20,287
  • 13
  • 71
  • 105
deepspace
  • 771
  • 3
  • 11
  • 25
  • If it's in the `for` loop, is it the fault of Lua API? – Bartek Banachewicz Aug 12 '13 at 14:57
  • Yes it's the fault of Lua API. – deepspace Aug 12 '13 at 14:59
  • 1
    @deepspace What do you mean it forgets the last element? The way your loop *iterates over* the node elements *does not* depend on i. Do you mean the *first* element appears to be missing because it's inserted at `t[0] = e->GetText()`? – greatwolf Aug 12 '13 at 17:59
  • I don't know. Just imagine I change **i** to 0 and it works but doesn't put the first element e->GetText()(const char*) to the table. With **i** set to 1 it shows Debug Assertion Failed but all elements that I need are in the table. I don't know why. Function works but program stops. I tried to use pushliteral instead of pushstring(I don't think it makes a difference) but compiler failed. – deepspace Aug 12 '13 at 18:39
  • 1
    just to help with debugging, can you have it push a dummy string for all elements? eg. something like `lua_pushstring(L, "blah foobar");` and comment out `lua_pushstring(L, e->GetText());` – greatwolf Aug 12 '13 at 18:47
  • 1
    In the second case try iterating with pairs, you should see all the elements. The text "debug assertion failed" is not present in Lua's source code. – Caladan Aug 12 '13 at 18:59
  • I built library again after few hours and now there's no error! I don't know what's going on. Everything works fine now. Maybe the IDE was bugged. I built when I restarted it. Thank's everyone :) peace. – deepspace Aug 12 '13 at 19:04
  • 1
    I suggest you clean up the topic not to confuse others. – Caladan Aug 12 '13 at 19:08
  • Not printing the first element when you use 0 and ipairs is expected. ipairs, matching the rest of lua, starts at 1 so ipairs simply doesn't see the 0 index/value. – Etan Reisner Aug 12 '13 at 19:45

1 Answers1

2

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);
}
Caladan
  • 1,471
  • 11
  • 13
  • Program crashes when I set the index on -3. It's on -2. I don't see the problem here. – deepspace Aug 12 '13 at 15:12
  • Did you try to debug it to see what's the call stack and how does the lua stack look like? – Caladan Aug 12 '13 at 15:14
  • Yes I tried and the problem is in the lua_rawseti(L,-3,i); I said that the table is on -2.(This is the next statement to execute) – deepspace Aug 12 '13 at 15:17
  • That's crazy. When I set *i* variable on 0 it goes like that 0, 1, 2 ,3 and there's not crash(it forgets about last element) but when it's on 1, prints all elements and assertion failed shows up. – deepspace Aug 12 '13 at 15:58
  • This example: http://lua-users.org/wiki/SimpleLuaApiExample shows that i should start with 1. Try to initialize i var with one and you'll see. – deepspace Aug 12 '13 at 16:29
  • @deepspace the link example starts at one because lua typically starts counting from 1 and not 0 like other languages. As far as `lua_rawseti` goes, that's just the same as `t[n] = v` in lua code (without metamethods). – greatwolf Aug 12 '13 at 17:32
  • I know that Lua starts from 1 but I mean that program doesn't work as it should when the I initialize **i** with 1: There's Debug Assertion Failed when I use lua_rawseti(L,-2,1); -- note 1. Function seems to be working but this ugly error shows up without any reason.. – deepspace Aug 12 '13 at 18:35
  • Can you track what causes this assertion? The message is "Debug assertion failed" without any additional message? No line number, no filename? – Caladan Aug 12 '13 at 18:49