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 208395
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T17:49:18+00:00 2026-05-11T17:49:18+00:00

OK so I have a C++ class that is exposed to Lua using SWIG.

  • 0

OK so I have a C++ class that is exposed to Lua using SWIG. The script creates the object but a manager class also has a pointer to the object so it can be modified in C++(or another script) for whatever reason.

The problem is that when the script finishes the object is freed, how can I control what the Garbage collector collects without having to implement a gc metamethod?

Here is an example:

--Script that creates the object
someObject = Utils.Object("Obj name");

Now the Object has registered itself with the manager so the rest of the application(and other scripts) can access it.

--Another script
obj = ObjManager:GetObject(0);

Clearly not a very realistic example but hopefully it illustrates my question. Is there a way to veto the garbage collector without a gc metamethod in C++?

Just to clarify the manager is in C++, and Utils is the module name housing the exposed class. Also the object registers itself to the manager in its constructor.

Thanks in advance.

  • 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-05-11T17:49:18+00:00Added an answer on May 11, 2026 at 5:49 pm

    Lua’s GC only knows about references held within Lua, which is a reasonable implementation constraint. This implies that an object’s lifetime is under Lua’s control. If an object created by executing one script or function needs to be available to later scripts or functions, a reference to it has to be preserved within the Lua state so that the GC knows it is still in use. Otherwise, it is indistinguishable from garbage, and may be discarded at any time.

    This one of the purposes of the Lua registry table. The C side can easily hold a reference to any Lua object by placing it in the registry table. The key can either be some unique value known to the C library (the address of a static variable converted to a light userdata is often a good choice since it cannot collide with any key from any other library). Alternatively the function call luaL_ref(L, LUA_REGISTRYINDEX) will put the item at the top of stack in the registry table and return a unique integer key. This works well for storing a script-provided callback function in a way that both protects the function from the GC and allows a “pointer” (the integer key) to it to be stored in a C structure so that it can be retrieved and called later.

    Note that luaL_ref() can be used to manage references in any table, so it may very well make sense to use a table that is private to your module for the purpose rather than the global registry table. In this case, the table ObjManager itself might be a good candidate.

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

Sidebar

Ask A Question

Stats

  • Questions 156k
  • Answers 156k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Option 1 allows the user to see more list items… May 12, 2026 at 10:56 am
  • Editorial Team
    Editorial Team added an answer I guess the event handler and the default behavior are… May 12, 2026 at 10:56 am
  • Editorial Team
    Editorial Team added an answer If you allocate the memory, then you are the one… May 12, 2026 at 10:56 am

Related Questions

The essence of the problem is, given a class hierarchy like this: class A
I have embedded Ruby inside my C++ application. I have generated the bindings using
I'd like a simple example of exporting a function from a C++ Windows DLL.
I have met an interesting problem while implementing the Observer pattern with C++ and

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.