Hi everyone,
How do i free up memory that I put to userdata/lightuserdata with malloc?
I have got a function that returns a userdata with a string.
Lua:
a=LPCHAR("Text")
Do not think i am silly :) I have got a reason for doing that.
But how to recognise when a gets a new value or a is destroyed? I have to free up the "Text".
Hope you understand my bad english :)
[EDIT]
Sorry wrong section. Could you please move it?
lua has an automatic garbage collector.
a ="Text"
in your case is a global, this only gets freeed when you tell so.
a = nil
but by declaring a 2 be a local.
local a = "Text"
now a gets stuffed in the garbage the moment the function is done.
this should always be used, as you can send the local 2 another function.
but if you want you can call the garbage collector when you want.
function Clear()
collectgarbage()
io.flush()
end
plop
Sorry, I think I was not very exact:
LPCHAR is a C function it calls something like:
ptr=malloc(sizeof(TCHAR)*(strlen(text)+1));
strcpy(ptr,text);
and returns the pointer ptr to lua. (lightuserdata)
The reason: I want to call dll functions by Lua. But Lua has no types like int, char and structs.
I wrote a library to convert to these types. And I use it like this:
lib=Library.Open("any.dll")
rect=convert.ToRECT(1,2,3,4)
text=convert.ToLPCHAR("Text")
lib:Run("anydllfunction",rect,text)
lib:Close()
text=nil;
rect=nil; --or something else
Now text and rect are destroyed but not the memory I allocated in my c function.
then this is a question for ppk/pta or sedules.
as nearly every1 here on the board just do lua scripting and have (like me) no idea of how things actualy work inside c/c++.
plop
free is your friend :))
Example
Quotechar *MSG;
MSG = (char *) malloc(256);
/*
do something here with MSG
*/
// and finally free memory :]
free(MSG);
So... Wrong forum too... ;)
But now I think I have found some source code that helps me. I hoped there would be a simple way like assign a function to __index of a metatable. But it seems much harder.
Thanks for help.
QuoteOriginally posted by DrGreenPepper
So... Wrong forum too... ;)
But now I think I have found some source code that helps me. I hoped there would be a simple way like assign a function to __index of a metatable. But it seems much harder.
Thanks for help.
With such questions go to //www.meka-meka.com they help you.