Skip to content

Commit

Permalink
Merge pull request #37 from rrthomas/master
Browse files Browse the repository at this point in the history
Fix deallocation of memory blocks
  • Loading branch information
mascarenhas committed Sep 24, 2013
2 parents bfab480 + 40ea3dd commit 708c48d
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions src/alien.c
Expand Up @@ -62,6 +62,9 @@
#define ALIEN__SPLICE(_s, _t) _s##_t
#define ALIEN_SPLICE(_s, _t) ALIEN__SPLICE(_s, _t)

#define LALLOC_FREE_STRING(lalloc, aud, s) \
(lalloc)((aud), (s), sizeof(char) * (strlen(s) + 1), 0)

#if LUA_VERSION_NUM == 502
static int luaL_typerror (lua_State *L, int narg, const char *tname) {
const char *msg = lua_pushfstring(L, "%s expected, got %s",
Expand Down Expand Up @@ -428,7 +431,7 @@ static int alien_library_get(lua_State *L) {
strcpy(name, funcname);
fn = alien_loadfunc(L, al->lib, funcname);
if(!fn) {
lalloc(aud, name, 0, 0);
LALLOC_FREE_STRING(lalloc, aud, name);
return lua_error(L);
}
alien_makefunction(L, al, fn, name);
Expand Down Expand Up @@ -599,7 +602,8 @@ static int alien_function_types(lua_State *L) {
abi = FFI_DEFAULT_ABI;
}
if(af->params) {
lalloc(aud, af->params, 0, 0); lalloc(aud, af->ffi_params, 0, 0);
lalloc(aud, af->params, sizeof(alien_Type) * af->nparams, 0);
lalloc(aud, af->ffi_params, sizeof(ffi_type *) * af->nparams, 0);
af->params = NULL; af->ffi_params = NULL;
}
af->nparams = lua_istable(L, 2) ? lua_objlen(L, 2) : lua_gettop(L) - 2;
Expand Down Expand Up @@ -790,7 +794,7 @@ static int alien_library_gc(lua_State *L) {
if(al->lib) {
alien_unload(al->lib);
al->lib = NULL;
if(al->name) { lalloc(aud, al->name, 0, 0); al->name = NULL; }
if(al->name) { LALLOC_FREE_STRING(lalloc, aud, al->name); al->name = NULL; }
}
return 0;
}
Expand All @@ -799,9 +803,9 @@ static int alien_function_gc(lua_State *L) {
alien_Function *af = alien_checkfunction(L, 1);
void *aud;
lua_Alloc lalloc = lua_getallocf(L, &aud);
if(af->name) lalloc(aud, af->name, 0, 0);
if(af->params) lalloc(aud, af->params, 0, 0);
if(af->ffi_params) lalloc(aud, af->ffi_params, 0, 0);
if(af->name) LALLOC_FREE_STRING(lalloc, aud, af->name);
if(af->params) lalloc(aud, af->params, sizeof(alien_Type) * af->nparams, 0);
if(af->ffi_params) lalloc(aud, af->ffi_params, sizeof(ffi_type *) * af->nparams, 0);
if(af->fn_ref) {
luaL_unref(af->L, LUA_REGISTRYINDEX, af->fn_ref);
ffi_closure_free(af->fn);
Expand Down

0 comments on commit 708c48d

Please sign in to comment.