|
Sorry, my terminology wasn't clear. When the user does A:delete(), the delete method calls a CFunction with self.Cptr which allows the C++ object to get deleted.
Ack. This really should be done with a __gc metamethod on the usedata object. How do you know if any other references to the same C object exist or not (aside from assumed implementation conventions)? Allowing a hanging C ptr that has been deleted explicitly is asking for trouble.
Then delete() sets self = nil and I leave the rest to the lua GC.
This isn't really useful.. setting self to nil won't cause the table to be cleaned up. Keep in mind 'self' is merely one reference to the table; any caller references will need to be cleaned up too.
That is to say, every call to delete would have to be followed by settting (all) references to nil:
A:delete() A = nil
So my question comes down to how to automatically get the Cfunction called with the object pointer so the C++ object can be free'd?
Right, so what you need to do is set a metatable on the userdata object with __gc defined. When the table is collected, and the last reference to that userdata goes away, it will (at some undefined time) be called.
- Peter