lua-users home
lua-l archive

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


Is it possible for userdata to tell the GC it has a (native) reference to another userdata object, and so prevent the GC to collect it?
Here's an example how i thought to use it:
 
 
-- returns a 'bitmap' userdata
bmp = gfx.LoadBitmap("test.bmp")
 
-- returns a 'font' userdata which depends on the bitmap
-- should tell the gc it has a reference to 'bmp'
font = gfx.CreateFont(bmp)
 
-- removes the lua side reference of the bitmap
bmp = nil
 
-- must not collect the bitmap since 'font' has a reference
collectgarbage()
 
-- remove font reference
font = nil
 
-- is allowed to collect the font
-- its finalizer releases its reference to the bitmap
collectgarbage()
 
-- is now allowed to collect the bitmap
collectgarbage()