lua-users home
lua-l archive

Re: Userdata and GC

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Thanks for the help. I think I was going along those lines, but I just wasn't sure. I'm planning on using Userdata to tuck away a pointer for some memory that I need to control, but pass it back and forth between C and Lua. The script is the "owner" of the pointer, since it is the only piece that needs/uses it and can tell me when it is no longer necessary. 
With that type of mechanism, how do I find out when the object is really no longer used by Lua? I assume I need to define my own GC function and tell Lua about it. The docs say you can define a GC metamethod for your Userdata, but I don't see any info on how to go about actually setting one up. Are there any C API sample programs or code snippets out there that demonstrate a Userdata GC function?
Thanks again-
 
 Ty
>You should never free memory which Lua has allocated. You should always 
>free memory which you have allocated. (Lua never "takes" or "gives" 
>ownership of memory. If you have a call to malloc() or free, you need a 
>matching call to free() or delete. Or whatever you use to allocate 
>resources.) It is really that simple.
>If you want to handle your own memory, use the userdata only to hold a 
>pointer to an object you have allocated.
>Hope this helps.
>R.

AltStyle によって変換されたページ (->オリジナル) /