I’m working on creating a binding for an object-based library[1] and am
trying to find a way to make it work well with the garbage collector.
Constructor functions from the library return a pointer to their data
structure, making it easy to wrap in a Ruby object using
Data_Wrap_Struct.
The problem I’m having is when one of the C objects takes responsibility
for it’s associated object(s). For example, a Statement[2] has three
member Nodes[3]. When the statement’s free function is called, it will
also free the associated nodes. There are functions to get and set each
of these nodes.
The problem I’m having is marking the associated nodes so the garbage
collector won’t prematurely free them. There may be zero or more Ruby
objects wrapping each one of the nodes, if any one of these is destroyed
the statement will enter an undefined state.
I’d like to define a mark function for the statement’s that will mark an
RData object pointing to a specific pointer. As I understand it,
rb_gc_mark will only mark a VALUE, is there a good way to do this?
An alternative I’m considering would be to create my own struct that
points to the librdf_statement as well as VALUEs for each of the nodes.
I’d have to write helper functions to make sure the VALUE is updated if
the statement is changed externally, but I think it should work.
The safe (and naive) approach is to copy anything going into or out of
the statement, but I’d rather not do that.
Thanks,
Justin
[1] http://librdf.org/
[2] RDF Triple (librdf_statement)
[3] RDF term (librdf_node)