lua-users home
lua-l archive

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


I'm trying to create userdata in library (struct Image) and collect it
using __gc metamethod:

C code:

  Image * newImage() {
    ptr = malloc(...);
    return ptr;
  }
  void deleteImage(Image * ptr) {
    //do something
    free(ptr->internal_data);
  }

Lua code:

  ffi.metatype('Image', {
	__index = {
		draw = lib.imageDraw
	}, 
	__gc = lib.deleteImage
  })

Using image:

  local ptr = ffi.new('Image')
  ptr:draw() --works
  ptr = nil --works perfectly, lib.deleteImage works
  -------------------------
  local ptr = lib.newImage()
  ptr:draw() --works as previous
  ptr = nil --here is no __gc call!
  collectgarbage() --still nothing

I use libraries, that generate many dynamic structures, is there a way
to connect library's finalizers with gc?

-- 
RPG