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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T12:00:21+00:00 2026-05-27T12:00:21+00:00

Im researching about how to integrate full script support inside my app but have

  • 0

Im researching about how to integrate full script support inside my app but have a bit of a problem planning my C API to be LUA friendly.

Basically I got bunch of structs that are created through init and free
functions like this:

[ test.h ]

typedef struct
{
char name[ 50 ];
} Test;


Test *TestAdd( char *name );

Test *TestDelete( Test *test );

[ test.c ]

Test *TestAdd( char *name )
{
Test *test = ( Test * ) calloc( 1, sizeof( Test ) );

strcpy( test->name, name );

return test;
}


Test *TestDelete( Test *test )
{
free( test );
return NULL;
}

Im using swig to generate the LUA module so I create the following interface file:

[ test.i ]

%module test
%{

%}

Test *TestAdd( char *name );

Test *TestDelete( Test * test );

Everything work fine if the user code is like this:

a = test.TestAdd( "test" )
a = test.TestDelete( a )

if( a != nil ) print( a.name )

But if the user code is like that:

a = test.TestAdd( "test" )
test.TestDelete( a )

if( a != nil ) print( a.name ) -- Crash the app with bad_access (not just a LuaVM error).

Or even worst:

a = test.TestAdd( "test" )
test.TestDelete( a )
test.TestDelete( a )
-- Another way of making crash my app completely!

Is there any way that I can create a set of API in C that would avoid this kind of problem and would allow the user to safely add/delete and access properties in a safe way that would not create “bad access” error and make the whole program crash, the best would be that the LUAVM simply return an error and the execution continue.

I’ve been searching and tried different approach to my C API to avoid this problem but failed…

Anybody can help me or give me some pointers about the direction to go with this.

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-27T12:00:22+00:00Added an answer on May 27, 2026 at 12:00 pm

    There is a way to do this: stop exporting C-style interfaces directly to Lua. C is not Lua, and you should never intend for Lua programs to act like C programs.

    Unless there is no way to avoid it, Lua code should never have to free anything. If Lua code explicitly creates something, then Lua code should have governance over its lifetime. That means you use garbage collection to delete the memory: when the Lua GC is finished with it, you use a metamethod to free the pointer with whatever call you use.

    In general, you give Lua a pointer to an object in one of two ways: either Lua owns that pointer, or that pointer is to an object that will outlive the lua_State itself (and therefore will always be available to the Lua script). There are occasions where you might hand Lua some temporary object that it can reference, but these should be avoided whenever possible.

    What you need to do is use the %newobject directive to tell SWIG that TestAdd returns a pointer that needs to be deleted. Then, you need to associate the Test object with TestDelete as the deleter, using %newfree typemap. This will properly transfer ownership of the Test from C to Lua. At which point, C should never delete it manually. Just let Lua and SWIG do their job.

    As such, TestDelete should not be exposed to Lua directly. It will be called implicitly when the GC detects that nobody is referring to the Test instance anymore.

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

Sidebar

Related Questions

I've been researching a bit about .NET Performance counters, but couldn't find anything detailed
I've been researching about building desktop application with JavaFX framework. Does anyone have any
We are writing this paper about database access in a web app and have
I have been researching about the headless browsers available till to date and found
sorry if this question sounds useless . I am researching about google app engine
I have been researching for a while now about how to implement an OAuth
I've been researching this all day but I haven't found anything about the rootViewController
I have been researching this problem and while there's lots of posts on various
I'm researching about dllexport and dllimport between C++ and C#. I have read this
I have been researching and have some few ideas about a distributed caching system

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.