Freeing memory?
 

News:

29 December 2022 - PtokaX 0.5.3.0 (20th anniversary edition) released...
11 April 2017 - PtokaX 0.5.2.2 released...
8 April 2015 Anti child and anti pedo pr0n scripts are not allowed anymore on this board!
28 September 2015 - PtokaX 0.5.2.1 for Windows 10 IoT released...
3 September 2015 - PtokaX 0.5.2.1 released...
16 August 2015 - PtokaX 0.5.2.0 released...
1 August 2015 - Crowdfunding for ADC protocol support in PtokaX ended. Clearly nobody want ADC support...
30 June 2015 - PtokaX 0.5.1.0 released...
30 April 2015 Crowdfunding for ADC protocol support in PtokaX
26 April 2015 New support hub!
20 February 2015 - PtokaX 0.5.0.3 released...
13 April 2014 - PtokaX 0.5.0.2 released...
23 March 2014 - PtokaX testing version 0.5.0.1 build 454 is available.
04 March 2014 - PtokaX.org sites were temporary down because of DDOS attacks and issues with hosting service provider.

Main Menu

Freeing memory?

Started by DrGreenPepper, 28 February, 2005, 20:42:03

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

DrGreenPepper

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?

plop

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
http://www.plop.nl lua scripts/howto\'s.
http://www.thegoldenangel.net
http://www.vikingshub.com
http://www.lua.org

>>----> he who fights hatred with hatred, drives the spreading of hatred <----<<

DrGreenPepper

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.

plop

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
http://www.plop.nl lua scripts/howto\'s.
http://www.thegoldenangel.net
http://www.vikingshub.com
http://www.lua.org

>>----> he who fights hatred with hatred, drives the spreading of hatred <----<<

PPK

free is your friend  :))
Example
Quotechar *MSG;
MSG = (char *) malloc(256);

/*
    do something here with MSG
*/

// and finally free memory :]
free(MSG);
"Most of you are familiar with the virtues of a programmer. There are three, of course: laziness, impatience, and hubris." - Larry Wall

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.

bastya_elvtars

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.
Everything could have been anything else and it would have just as much meaning.

SMF spam blocked by CleanTalk