[Rubydotnet-developer] GC ideas
Status: Alpha
Brought to you by:
thomas
From: Tim S. <ti...@ih...> - 2004-01-08 11:40:10
|
Hello. I changed the way I deal with the GC in my rubydotnetproxy object. (I haven't uploaded a version using these new changes.) I was previously using reference counting[*]. This was error prone and ugly. (It was not easy to be sure that I hadn't made a mistake somewhere.) [*] This would all be easy if I had a rule like "for each VALUE, there is at most one RbType wrapping it", but then I couldn't use implicit constructors for type conversions. Now for .NET calling Ruby: In .NET I have a collection of weakreferences of RbTypes. (I add to it whenever a RbType is constructed.) Whenever Ruby's GC is running, I call rb_gc_mark for each VALUE where the RbType has not yet been collected. (And remove those which have been collected.) In this way, as long as .NET has access to the RbType, Ruby will not collect the associated VALUE. For Ruby calling .NET In .NET I have a hashtable from .NET OBJ id => object. Roughly as often as the .NET GC is run, I create a C array of OBJs, assign it to a global variable and then rb_gc_start(). My mark_DotNetData() function adds its type_id/instance_id to the array. Then I pass this array to .NET and it removes all objects from the hashtable whose ids don't appear in the array. This still suffers from the circular references memory leak that reference counting had. BTW: I saw (on a webpage somewhere? I seem to recall it was Thomas') someone say they added a feature to allow you to do things like the following Ruby require 'dotnet' class Foo end foo = Foo.new h = Hashtable.new h[foo] = 12345 i.e. have arbitrary Ruby objects as arguments to .NET methods by wrapping them up as `.NET wrapper around Ruby object` and unwrapping on the other end. This seems like a really great idea. It does raise issues like "should we still convert arguments which are Ruby strings into .NET strings automatically. Similarly for integers etc. -- Q: What is normed, complete, and yellow? A: A Bananach space... |