Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7869691
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T01:24:24+00:00 2026-06-03T01:24:24+00:00

In the Lua 5.2 manual we can find the following text : When a

  • 0

In the Lua 5.2 manual we can find the following text:

When a C function is created, it is possible to associate some values
with it, thus creating a C closure (see lua_pushcclosure); these
values are called upvalues and are accessible to the function whenever
it is called.

Whenever a C function is called, its upvalues are located at specific
pseudo-indices. These pseudo-indices are produced by the macro
lua_upvalueindex. The first value associated with a function is at
position lua_upvalueindex(1), and so on. Any access to
lua_upvalueindex(n), where n is greater than the number of upvalues of
the current function (but not greater than 256), produces an
acceptable (but invalid) index.

So, I created a callback function associated with an object pointer created with “new”.

lua_pushstring(L,"myCallbackFunc");
Foo* ideleg = new Foo()
lua_pushlightuserdata (L, (Foo*)ideleg);
lua_pushcclosure(L, LuaCall<Tr,C,Args...>::LuaCallback,1);
lua_settable(L,-3);

And all the mechanics works very nicely… but now it’s time to clean up, and I’m unable to get the pointer back so I can delete it, when I unregister the callback function or when I quit my program.

I do find the table entry using the following snippet:

lua_pushnil(L);
while (lua_next(L, -2) != 0)
{
    if (lua_isstring(L, -2) && lua_tostring(L,-2) == "myCallBackFunc" ) 
    {
        // get the pointer back and delete it!
    }
}

(Can I do it using lua_getfield(L, -1, "myCallBackFunc"); ?)

But I’m unable to get the upvalue associated to the cclosure outside the LuaCall<Tr,C,Args...>::LuaCallback() function (Indeed inside this close function I can simply use lua_upvalueindex(1) but in this case I’m outside the close function…)

Is there a way to get this value so I can delete the pointer when I do not need it anymore ?


Edit: I did found the lua_getupvalue function that should do what I need, but at the moment I don’t know how to get the cclosure stack index. So it still doesn’t works yet.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T01:24:25+00:00Added an answer on June 3, 2026 at 1:24 am

    Yes, you can do this in Lua. But I strongly advise against it.

    If you have access to that callback, if it’s on the Lua stack, then it is entirely possible that some bit of Lua code has access to it as well. Which means that even though you think it will be destroyed, it actually won’t be until Lua finishes with it. Destroying your closure puts it into a non-functioning state. Yet, because Lua may still have access to it, it is possible for the closure to be called after you destroyed its upvalue.

    Badness ensues.

    It would be better to put the pointer into a full userdata and attach a metatable to it with a __gc metamethod to do cleanup. That way, you can be certain that it will be be cleaned up when it truly is no longer in use.

    However, if you insist on doing it your way, you can always use lua_getupvalue.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does Lua have a builtin sum() function? I can't seem to find one, and
Lua's tonumber function is nice but can only convert unsigned integers unless they are
In Lua, you can create a table the following way : local t =
In Lua the the __index metamethod can be either a function or a table
Lua has a really nice no-parenthesis call syntax that coupled with function closures allow
#1 Lua: local test = Test(); #2 C: //creating lua's test luaL_newmetatable(L, someTable); lua_userdata
Does Lua provide a function to make the first character in a word uppercase
I can't init lua correctly under Arch Linux. Lua - latest version. Here is
I was reading the lua manual (looking for something) and I found out that
Lua functions can return multiple results : a, b, c = unpack({'one', 'two', 'three'})

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.