# [Lua/C++] Table of closures

This topic is 2561 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I'm trying to put closures into tables but it keeps crashing. This isn't my exact code but it does pretty much the same thing:
 // create new table lua_newtable( L ); int Table = lua_gettop( L ); // add closure 1 with 2 upvalues lua_pushstring( L, "Closure1" ); lua_pushinteger( L, 1 ); lua_pushinteger( L, 2 ); lua_pushcclosure( L, Function, 2 ); lua_settable( L, Table ); // add closure 2 with 1 upvalue lua_pushstring( L, "Closure2" ); lua_pushinteger( L, 1 ); lua_pushcclosure( L, Function, 1 ); lua_settable( L, Table ); // set the name of the table lua_setglobal( L, "Table1" ); 

The table should work like this:
[font="Courier New"]Table1.Closure1()
Table1.Closure2()[/font]

It always crashes at the lua_settable calls. I believe the reason for this is because the stack is supposed to be arranged with the key before the value, but doing that would mess up the closure.
Anyone know how to get around this?

Thanks.

##### Share on other sites
"[color=#1C2837][size=2]This isn't my exact code but it does pretty much the same thing" Please post the actual code or code which displays the problem as this code is fine.
[color=#1C2837][size=2]

[color=#1C2837][size=2]"[color=#1C2837][size=2]I believe the reason for this is because the stack is supposed to be arranged with the key before the value" That is what the stack looks like in your example.

##### Share on other sites
Ah, it turns out I was putting in the wrong integer into lua_pushcclosure. Works now. Thanks for the help.

1. 1
Rutin
24
2. 2
JoeJ
20
3. 3
4. 4
5. 5

• 9
• 46
• 41
• 23
• 13
• ### Forum Statistics

• Total Topics
631749
• Total Posts
3002045
×