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

  • SEARCH
  • Home
  • 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 7398171
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T03:50:17+00:00 2026-05-29T03:50:17+00:00

I am using cocos2d-x with Lua for development. Recently I used Instruments and have

  • 0

I am using cocos2d-x with Lua for development. Recently I used Instruments and have discovered some amount of memory which is abandoned:
cocos2d-x performs release on objects in its own autorelease pool, but I still have reference coming from Lua.
How to figure out when those references are made?
I need to free that memory to avoid abandoned memory allocations left in space. The only thing that comes to my mind is to use some Lua debugger to look after not nil Lua pointers.

If I only knew which Lua reference holds the pointer, I could release it manually then.

  • 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-29T03:50:18+00:00Added an answer on May 29, 2026 at 3:50 am

    Your question was a bit oddly worded. But as I understand it, your Lua script has references to objects allocated in external code. And as long as Lua has these references, the external code won’t free those resources. And you believe that those resources should be freed.

    The first thing you should check is that your Lua interface code uses metamethods to properly clean up references. That is, when Lua is finished with an object reference, there’s a metamethod attached (__gc) that will inform the external code that Lua is finished with the object.

    For the rest of this post, I will assume that this code exists and functions properly. You should verify this yourself.

    Given that assumption, what you’re seeing happens for one of two reasons:

    1. Your Lua code has finished using all of its references, but hasn’t cleaned them up yet. By this, I mean that Lua scripts no longer have the references in any local variables, global variables, etc. In this case, what’s happened is that Lua’s garbage collector simply hasn’t run and cleaned up stuff yet. So you’ll need to do that with lua_gc(L, LUA_GCCOLLECT, 0);. If this is the case, then after running this function all references from Lua should have been cleaned up.

    2. Your Lua code still has active references to external objects lurking about.

    Dealing with case #2 is difficult. It is the responsibility of Lua code to be… responsible. It should store references when it needs to refer to stuff, and then forget about those references afterwards. By that, I mean don’t store the value in a non-local variable (and by “non-local”, I also mean to avoid locals that are used by nested function definitions).

    There is no means to find where those references still exist; after all, Lua values do not necessarily have a name. Also, Lua doesn’t really give you a way to iterate through every possible variable in the lua_State. Even if there was, there is certainly no means of simply blanking those references out. After all, Lua code can still touch them. If you blanked them, Lua code would crash when it tried to talk to those objects. So even if you could do what you wanted, it would be counter-productive.

    I would suggest one of three methods for dealing with this:

    1. Exercise discipline in your scripting. Make sure you know where all of your non-Lua objects are stored. Make sure that these objects are released when they should be. Know where you put these external objects, and make sure that they are released on time. By “released,” I mean simply overwriting their value with nil.

    2. Build a layer of insulation between your script and your source code. Rather than directly giving Lua pointers to Cocos2D-X objects, give it pointers to a special object that may contain a reference to a Cocos2D-X object. That way, you can control when these objects get released directly by telling this control object to drop its reference. If Lua calls an empty control object, the function will return… something innocuous (so that you don’t crash). Obviously, Lua should have a function to see if it is a valid control object.

    3. Do whatever you want in Lua, but then destroy the entire Lua state when it’s time to free resources. Your current method is very fine-grained. Lua creates some resources, and Lua then decides when they go away. A more strict resource management scheme would be for Lua scripts to load some resources and reference resources. But then when it’s time to destroy all of those resources, you just lua_close the lua_State itself. That will release all references, thus releasing those resources. Problem solved.

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

Sidebar

Related Questions

i have an app using cocos2d, Im moving some sprites and having animations when
I'm using cocos2d for mac (1.0.1) I have these two methods (which are similar
I am using Cocos2D for my main framework. In some cases, I want Cocos2D
I'm using cocos2d with objective C. I have a class called CrystalineBubble that is
I need some information about using cocos2d in iPad. Can we use 2048x2048 sprite
I am using cocos2d to build a game. I have an array of CCSprites
In a game that I am making whilst using Cocos2d, I have a sprite
am having uiviewcontroller for some effects using in camera. am using cocos2d lot for
I'm using cocos2d-x [lua binding] to port my cocos2d [objective-c] game and I had
Currently using cocos2d. I have a plist data name myplist.plist. Inside the plist are

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.