Thread: allocation issues (via lua c api)

  1. #1
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,733

    allocation issues (via lua c api)

    Had a bunch of issues setting up my globals but I finally resolved them only to be hit with this issue, I get a segmentation fault whenever lua calls this function:
    Code:
    void *foo_alloc( void *ud, void *ptr, size_t size, size_t want ) {
    	uchar *tmp = NULL;
    	if ( !want ) {
    		if ( ptr ) free( ptr );
    		return NULL;
    	}
    	if ( !ptr ) {
    		tmp = malloc( want );
    		if ( !tmp ) goto nomem;
    		(void)memset( tmp, 0, want );
    		return tmp;
    	}
    	tmp = realloc( ptr, want );
    	if ( !tmp ) goto nomem;
    	if ( size < want ) memset( &(tmp[size]), 0, want - size );
    	return tmp;
    	nomem:
    	errno = ENOMEM;
    	return NULL;
    }
    at this point in my lua file:
    Code:
    function tostr(val)
    	local txt = ""
    	if type(val) == "table" then
    		txt = txt .. "{\n"
    		for i,v in next,val do
    			txt = txt .. "  member '" .. i .. "' = " .. (tostr(v)) .. "\n"
    		end
    		return txt .. "}"
    	elseif val then return "" .. val
    	else return "nil" end
    end
    local foo_names = {"foo1","foo2","foo3"}
    local foo_id = nil
    print("Testing tostr()...") -- This is the last output before the crash
    print("foo_names = " .. (tostr(foo_names)))
    I've already confirmed that this same code works with the default lua executable /bin/lua (or at least that's where I think the executable is, I just run lua foo.lua), so I'm certain it's an issue with my code, any ideas?

    Edit: To be clear gede catches the seg fault at the call to realloc()
    Last edited by awsdert; 01-01-2020 at 07:31 AM.

  2. #2
    Registered User
    Join Date
    Dec 2017
    Posts
    1,628
    Since there doesn't seem to be anything wrong with your code from a C perspective, it probably has something to do with how it's being called from the lua interpreter. In particular, how do you know the ud parameter is properly set?
    Code:
    void *foo_alloc( void *ud, void *ptr, size_t size, size_t want ) {
        char *mem = NULL;
        if ( want == 0 )
            free( ptr );  // free is okay with NULL ptr
        else {
            mem = realloc( ptr, want ); // realloc is okay with NULL ptr
            if ( !mem )
                errno = ENOMEM;
            else if ( size < want )
                memset( &(mem[size]), 0, want - size );
        }
        return mem;
    }
    A little inaccuracy saves tons of explanation. - H.H. Munro

  3. #3
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,733
    That ud just stands for user data, I do nothing with it because I passed NULL to lua_newstate for that variable, lua doesn't care whether it's NULL or a valid pointer, it just passes it onto the alloctor, it's probably there for things like HeapAlloc(), any I seem to have fixed it, it now looks like this:
    Code:
    static void* foo_alloc( void *ud, void *ptr, size_t size, size_t want )
    {
    	uchar *tmp = NULL;
    	(void)ud;
    	errno = EXIT_SUCCESS;
    	if ( want == size ) return ptr;
    	if ( !want ) {
    		if ( ptr ) free( ptr );
    		return NULL;
    	}
    	if ( !ptr ) tmp = malloc( want );
    	else tmp = realloc( ptr, want );
    	if ( !tmp ) {
    		if ( want < size ) {
    			tmp = malloc( want );
    			if ( tmp ) memcpy( tmp, ptr, want );
    			free(ptr);
    			return tmp;
    		}
    		goto nomem;
    	}
    	if ( size < want ) memset( &tmp[size], 0, want - size );
    	return tmp;
    	nomem:
    	if ( errno == EXIT_SUCCESS ) errno = ENOMEM;
    	return NULL;
    }
    But now I've run into a new problem, the app keeps crashing at around the end part:
    Code:
    void foo_gnewcfunc( lua_State *L, char *name, lua_CFunction func ) {
    	lua_pushcfunction(L,func);
    	lua_setglobal(L,name);
    	lua_pop(L,2);
    }
    void foo_gnewtable( lua_State *L, char *name ) {
    	lua_newtable(L);
    	lua_setglobal(L,name);
    	lua_pop(L,2);
    }
    void foo_gnewstring( lua_State *L, char *name, char *value ) {
    	lua_pushstring(L,value);
    	lua_setglobal(L,name);
    	lua_pop(L,2);
    }
    void foo_gnewinteger( lua_State *L, char *name, long value ) {
    	lua_pushinteger(L,value);
    	lua_setglobal(L,name);
    	lua_pop(L,2);
    }
    void foo_gnewnumber( lua_State *L, char *name, long double value ) {
    	lua_pushnumber(L,value);
    	lua_setglobal(L,name);
    	lua_pop(L,2);
    }
    ...
    int main( int argc, char *argv[] ) {
    	lua_State *L = NULL;
    	puts("Opening lua state...");
    	if ( !(L = lua_newstate(foo_alloc,NULL)) ) {
    		printf("error 0x%08X '%s'\n", errno, strerror(errno) );
    		return EXIT_FAILURE;
    	}
    	g_L = L;
    	puts("Setting atexit to cope with glut aborting program...");
    	atexit(foo_atexit);
    	puts("Opening lua libs...");
    	luaL_openlibs(L);
    	puts("Adding lua global locate()...");
    	foo_gnewcfunc(L,"locate",foo_locate);
    	puts("Adding lua global hookpid()...");
    	foo_gnewcfunc(L,"hookpid",foo_hookpid);
    	puts("Adding lua global table game...");
    	foo_gnewtable(L,"game");
    	puts("Adding lua global table glut...");
    	foo_gnewtable(L,"glut");
    	puts("Putting glut at top of lua stack...");
    	lua_getglobal(L,"glut");
    	puts("Adding string field api...");
    	foo_tnewstring(L,-1,"api","freeglut");
    	puts("Adding field initWindowPosition()...");
    	foo_tnewcfunc(L,-1,"initWindowPosition",foo_glutInitWindowPosition);
    	puts("Adding field initWindowSize()...");
    	foo_tnewcfunc(L,-1,"initWindowSize",foo_glutInitWindowSize);
    	puts("Adding field init()...");
    	foo_tnewcfunc(L,-1,"init",foo_glutInit); // Furthest I get is here
    	puts("Adding field terminate()...");
    	foo_tnewcfunc(L,-1,"terminate",foo_glutTerminate);
    	lua_pop(L,1);
    	puts("Attempt to run file foo.lua...");
    	luaL_dofile(L,"foo.lua");
    	lua_close(L);
    	g_L = NULL;
    	puts("Nothing further to do, leaving program...");
    	return EXIT_SUCCESS;
    }
    Edit: Just removed the lua_pop(L,2) calls and everything went dandy

  4. #4
    Registered User
    Join Date
    Dec 2017
    Posts
    1,628
    If you actually read my post then why does your code still look like crap? You are obviously the kind of person who can't really learn anything. I'll leave you to your own devices from now on.
    A little inaccuracy saves tons of explanation. - H.H. Munro

  5. #5
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,733
    Quote Originally Posted by john.c View Post
    If you actually read my post then why does your code still look like crap? You are obviously the kind of person who can't really learn anything. I'll leave you to your own devices from now on.
    Rude, if you just gonna be an .............. then get off these forums

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory Allocation issues
    By Otto45 in forum C Programming
    Replies: 2
    Last Post: 03-14-2013, 06:30 AM
  2. mem allocation in c
    By vvaarwik in forum C Programming
    Replies: 2
    Last Post: 02-03-2011, 11:55 PM
  3. Allocation
    By EvN in forum C Programming
    Replies: 7
    Last Post: 08-28-2007, 10:39 AM
  4. memory allocation
    By afotoohi in forum C Programming
    Replies: 1
    Last Post: 03-04-2003, 10:21 PM
  5. memory allocation
    By rotis23 in forum C Programming
    Replies: 6
    Last Post: 10-04-2002, 03:49 PM

Tags for this Thread