From: Ali Polatel Date: 2010-05-25T14:55:32+09:00 Subject: The order of garbage collection --=-=-= I'm writing a Ruby extension wrapping a library that uses talloc for its memory allocation. I'm trying to make ruby's gc play nice with it. Here's a summary of the extension objects I'm dealing with: Database (the main object) Directory (returned by a method of Database) FileNames (returned by a method of Directory) Query (returned by a method of Database) Threads (returned by a method of Query) Thread (returned by a method of Threads) Messages (returned by a method of either Query or Threads or Thread) Message (returned by a method of either Database or Messages) Tags (returned by a method of either Thread or Message or Messages) The problem is when the parent of an object is freed, the object is freed automatically as well. For example when a Threads object is freed, all the Thread objects that belong to it are freed automatically. This means when the Database is freed all, or closed by the user, the underlying objects are freed automatically by talloc. This means an object may only be freed if its parent hasn't been freed yet. So far so good, to make ruby's gc play nice with this way of allocation, every struct that represents an object has an element VALUE parent and it's marked using rb_gc_mark(object->parent) in the mark phase. This problem arises when e.g. I have a database and message object and both of them go out of scope. Ruby-1.8 frees the message first and then the database but Ruby-1.9 does the opposite thus my extension blows. How can I ensure the objects are freed in order, from children to parent? --=-=-= Content-Type: application/pgp-signature -----BEGIN PGP SIGNATURE----- Version: GnuPG v2.0.14 (FreeBSD) iEYEARECAAYFAkv7Zk8ACgkQQU4yORhF8iC8qgCgwTPm5CMCTwfMOMcFsh97O00+ 9LAAoJKGkiw7ntEjnGkM8gA9jaA3sH9O =Vy6t -----END PGP SIGNATURE----- --=-=-=--