# Garbage Collection: Marking RData ptr

**URL:** <https://www.ruby-forum.com/t/garbage-collection-marking-rdata-ptr/68869>\
**Category:** Ruby\
**Created:** [September 16, 2006, 2:13am UTC](https://www.ruby-forum.com/t/garbage-collection-marking-rdata-ptr/68869 "2006-09-16T02:13:09Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![vtphysguy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/77aa72/32.png) [@vtphysguy](https://www.ruby-forum.com/u/vtphysguy)\
**Post date:** [September 16, 2006, 2:13am UTC](https://www.ruby-forum.com/t/garbage-collection-marking-rdata-ptr/68869/1 "2006-09-16T02:13:09Z")

</div>

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/](http://librdf.org/)  
[2] [RDF Triple (librdf\_statement)](http://librdf.org/docs/api/redland-statement.html)  
[3] [RDF term (librdf\_node)](http://librdf.org/docs/api/redland-node.html)

---

<div class="post-metadata">

**Author:** ![vtphysguy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/77aa72/32.png) [@vtphysguy](https://www.ruby-forum.com/u/vtphysguy)\
**Post date:** [September 16, 2006, 4:06pm UTC](https://www.ruby-forum.com/t/garbage-collection-marking-rdata-ptr/68869/2 "2006-09-16T16:06:34Z")

</div>

> > > > > “J” == Justin B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

J\> The safe (and naive) approach is to copy anything going into or out  
of  
J\> the statement, but I’d rather not do that.

Well, what do you do is someone re-use a node after it was set in the  
statement ?

You have

The object passed in becomes owned by the statement object and must  
not  
^^^^^^^^  
be used by the caller after this call.  
^^^^^^^

and

This method returns a SHARED pointer to the object which must be copied  
by  
^^^^^^^^^^^^^^  
the caller if needed.

–

Guy Decoux

---

<div class="post-metadata">

**Author:** ![vtphysguy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/77aa72/32.png) [@vtphysguy](https://www.ruby-forum.com/u/vtphysguy)\
**Post date:** [September 16, 2006, 7:39pm UTC](https://www.ruby-forum.com/t/garbage-collection-marking-rdata-ptr/68869/3 "2006-09-16T19:39:57Z")

</div>

Redland nodes are immutable so it’s not really a problem. I just have  
to make sure that any object that takes possession of or depends on a  
node makes a copy for itself. In practice this is just statements, for  
which I have to do this anyway.

I was looking for a more efficient way to provide access to the the  
nodes in a statement than copying the underlying datastructure and  
rewrapping it on every access. I think I’ll leave it as is for now and  
if it becomes a performance problem I can cache the nodes in the object.

Unless of course there’s away to mark all RData objects wrapping a  
particular pointer 🙂

-Justin
