Author Topic: Freeing memory?  (Read 2546 times)

0 Members and 1 Guest are viewing this topic.

Offline DrGreenPepper

  • Newbie
  • *
  • Posts: 3
  • Karma: +0/-0
Freeing memory?
« on: 28 February, 2005, 20:42:03 »
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?
« Last Edit: 28 February, 2005, 23:02:55 by DrGreenPepper »

PtokaX forum

Freeing memory?
« on: 28 February, 2005, 20:42:03 »

Offline plop

  • Forum God
  • ****
  • Posts: 2 464
  • Karma: +37/-0
(No subject)
« Reply #1 on: 01 March, 2005, 01:11:34 »
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.
Code: [Select]
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 <----<<

Offline DrGreenPepper

  • Newbie
  • *
  • Posts: 3
  • Karma: +0/-0
(No subject)
« Reply #2 on: 01 March, 2005, 14:02:30 »
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.

Offline plop

  • Forum God
  • ****
  • Posts: 2 464
  • Karma: +37/-0
(No subject)
« Reply #3 on: 01 March, 2005, 23:47:09 »
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 <----<<

Offline PPK

  • Administrator
  • Emperor
  • *****
  • Posts: 1 480
  • Karma: +209/-22
  • PtokaX developer
(No subject)
« Reply #4 on: 02 March, 2005, 00:01:41 »
free is your friend  :))
Example
Quote
char *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

Offline DrGreenPepper

  • Newbie
  • *
  • Posts: 3
  • Karma: +0/-0
(No subject)
« Reply #5 on: 02 March, 2005, 00:12:06 »
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.

Offline bastya_elvtars

  • Forum God
  • ****
  • Posts: 3 744
  • Karma: +173/-7
  • The rock n' roll doctor
    • The FreshStuff3 Site
(No subject)
« Reply #6 on: 02 March, 2005, 00:44:29 »
Quote
Originally 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 http://www.meka-meka.com they help you.
Everything could have been anything else and it would have just as much meaning.

PtokaX forum

(No subject)
« Reply #6 on: 02 March, 2005, 00:44:29 »