Skip to content

Commit

Permalink
Fixed memory leaks problems consequence of binded methods finshing wi…
Browse files Browse the repository at this point in the history
…th explicit 'return number;' expression.
  • Loading branch information
pakozm committed May 19, 2015
1 parent d297158 commit 91312c6
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 9 deletions.
22 changes: 17 additions & 5 deletions binding/bind_templates/luabind_template.cc
Original file line number Diff line number Diff line change
Expand Up @@ -275,9 +275,10 @@ int lua_delete_$$ClassName$$_$$FILENAME2$$(lua_State *L){
// Hacemos un DecRef para borrar la referencia a este objeto
DecRef(obj);
}
// FIXME: This warning is due to the META_INSTANCE table, which is a metatable
// of itself and has a __gc method, so at the end of APRIL execution, the __gc
// is executed with a NULL pointer
// FIXME: This warning is due to the META_INSTANCE tables with other
// META_INSTANCEs as metatable (inheritance), so a __gc metamethod which is
// executed at the end of APRIL execution, receiving __gc call a table
// as argument, leading into obj=0 when calling to lua_rawget...
else {
DEBUG_OBJ("lua_delete_$$ClassName$$ WARNING!! NULL pointer", obj);
}
Expand Down Expand Up @@ -459,6 +460,18 @@ void bindluaopen_$$ClassName$$_$$FILENAME2$$(lua_State *L){
DEBUG("bindluaopen_$$ClassName$$_$$FILENAME2$$ (end)");
}

// to avoid early deletion of the object in case of garbage collection
struct luabind_reference_handler_$$ClassName$$ {
$$ClassName$$ *ref;
luabind_reference_handler_$$ClassName$$($$ClassName$$ *ref) :
ref(ref) {
IncRef(ref);
}
~luabind_reference_handler_$$ClassName$$() {
DecRef(ref);
}
};

//LUA for MethodName,code in pairs(class.methods) do
#undef FUNCTION_NAME
#define FUNCTION_NAME "$$(LUANAME[ClassName] or ClassName)$$:$$MethodName$$"
Expand All @@ -475,13 +488,12 @@ int lua_call_$$ClassName$$_$$MethodName$$(lua_State *L){
int luabind_num_returned_values = 0;
DEBUG_OBJ("lua_call_$$ClassName$$_$$MethodName$$ (begin)", obj);
// to avoid early deletion of the object in case of garbage collection
IncRef(obj);
luabind_reference_handler_$$ClassName$$ ref_handler(obj);
lua_remove(L,1);
// CODE:
{
$$code$$
}
DecRef(obj);
DEBUG_OBJ("lua_call_$$ClassName$$_$$MethodName$$ (end)", obj);
return luabind_num_returned_values;
}
Expand Down
1 change: 0 additions & 1 deletion packages/basics/dataset/binding/bind_dataset.lua.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1370,7 +1370,6 @@ LUABIND_ERROR("use constructor methods: matrix, etc.");
int argn = lua_gettop(L); // number of arguments
LUABIND_CHECK_PARAMETER(1, DataSetFloat);
DataSetFloat *ds;
const char *major_str;
LUABIND_GET_PARAMETER(1, DataSetFloat, ds);
obj = new DataSetFloat2TokenWrapper(ds);
LUABIND_RETURN(DataSetFloat2TokenWrapper, obj);
Expand Down
4 changes: 1 addition & 3 deletions packages/trainable/lua_src/trainable.lua
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,6 @@ trainable.dataset_multiple_iterator =
bunch_size = { type_match = "number", mandatory = true },
shuffle = { isa_match = random, mandatory = false, default=nil },
replacement = { type_match = "number", mandatory = false, default=nil },
bunch_major = { type_match = "string", mandatory = false, default="row_major" },
assert_pattern_sizes = { type_match = "table", mandatory = false,
default={ } },
}, t)
Expand All @@ -151,8 +150,7 @@ trainable.dataset_multiple_iterator =
nump, ds:numPatterns())
else nump = ds:numPatterns()
end
return is_a(ds,dataset) and dataset.token.wrapper(ds,
params.bunch_major) or ds
return is_a(ds,dataset) and dataset.token.wrapper(ds) or ds
end):
table(), nump
end
Expand Down

0 comments on commit 91312c6

Please sign in to comment.