Memory management in C++ Extension
-
I first typedefed VALUE and ID because of some include issues with ruby.h there. But it seems to be over now.
I changed the code to allocate the wrapped C++ class using
rb_define_alloc_func
but that didn't solved the issue.I get the memory error whenever I call load_model a second time, and need to do before some cleaning in the Reset() method of dynamically allocated memory (EntityInfo) in the tree.
-
Should it be "<" instead of "<=" ?
for(int i = 0; i **%(#0000FF)[<]** EntityTree.depth(it2)-rootdepth; ++i)
The original code:
void Coll;;Reset(void) { TreeEntity;;post_order_iterator it2 = EntityTree.begin_post(); TreeEntity;;post_order_iterator end2 = EntityTree.end_post(); int rootdepth = EntityTree.depth(it2); while(it2 != end2) { for(int i = 0; i <= EntityTree.depth(it2)-rootdepth; ++i) if (it2.node->data) delete it2.node->data; ++it2; } }
-
@olilej said:
Visual Studio allows to go in debug mode, so I don't see any exception happening.
You don't see the crash when you attach Visual Studio and run it?
-
I finally found my silly mistake using static keyword here, it was getting in the way of the GC.
%(#404080)[VALUE CRubyUtils::GetDAttribute(VALUE obj, VALUE att) { ~~static~~ VALUE getatt = rb_intern("get_attribute"); ~~static~~ VALUE dicatt = rb_str_new2("dynamic_attributes"); return rb_funcall(obj, getatt, 2, dicatt, att); }]
Thanks for your support, might see you at Basecamp.
-
Hmm.. did you have to remove static for the rb_intern call as well? I can understand that making the reference to the string object you created static could cause problems, but the rb_intern call?
-
From what I understand, static function variables persists out of the scope of the function and are only allocated once in a specific memory area.
As this memory was allocated using a VALUE pointer unknown from Ruby, the Garbage Collector cleaned it up and I ended up with a Memory Access Violation.So I would say, it is not a good idea to use a static variable of type VALUE, unless you find a way to tell Ruby GC by marking it.
-
No, thats not quite right.
You've made those variables static (FWIW, for no benefit) - but because their scope is limited to that function, nothing outside can access them so you should be fine with that.
The golden rule is never hang onto any VALUE reference beyond the scope of your functions.
When you get more experienced with Ruby extensions, you may want to look into using rb_gc_register_address() but I would strongly suggest you stick to never holding VALUE references unless you really understand whats going on under the hood.
Adam
-
No, true values (as opposed to references) - IDs etc - are fine to cache.
So method selectors can happily be cached once at startup. Just as Thomthom says, avoiding the additional function calls is a "Good Thing (tm)".
-
Thanks for clarifying Adam.
I have seen ID and VALUE mixed in Ruby C Extensions - which there is no compiler warning about since they are typedefs of the same type:
http://rxr.whitequark.org/mri/source/include/ruby/ruby.h#082
-
rb_intern doesn't return VALUE, it returns ID. Adam, would the same issues for VALUE be valid for ID?
I've used to make static const out of ID's I use a lot for easy access and avoid Ruby lookup up for every function call.
Advertisement