sketchucation logo sketchucation
    • Login
    ℹ️ Licensed Extensions | FredoBatch, ElevationProfile, FredoSketch, LayOps, MatSim and Pic2Shape will require license from Sept 1st More Info

    Memory management in C++ Extension

    Scheduled Pinned Locked Moved Developers' Forum
    15 Posts 4 Posters 753 Views 4 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • O Offline
      olilej
      last edited by

      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.

      1 Reply Last reply Reply Quote 0
      • icehuliI Offline
        icehuli
        last edited by

        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;
        	}
        }
        
        1 Reply Last reply Reply Quote 0
        • tt_suT Offline
          tt_su
          last edited by

          @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?

          1 Reply Last reply Reply Quote 0
          • O Offline
            olilej
            last edited by

            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.

            1 Reply Last reply Reply Quote 0
            • tt_suT Offline
              tt_su
              last edited by

              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?

              1 Reply Last reply Reply Quote 0
              • O Offline
                olilej
                last edited by

                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.

                1 Reply Last reply Reply Quote 0
                • AdamBA Offline
                  AdamB
                  last edited by

                  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

                  Developer of LightUp Click for website

                  1 Reply Last reply Reply Quote 0
                  • AdamBA Offline
                    AdamB
                    last edited by

                    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)". πŸ˜„

                    Developer of LightUp Click for website

                    1 Reply Last reply Reply Quote 0
                    • tt_suT Offline
                      tt_su
                      last edited by

                      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

                      1 Reply Last reply Reply Quote 0
                      • tt_suT Offline
                        tt_su
                        last edited by

                        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.

                        1 Reply Last reply Reply Quote 0
                        • 1 / 1
                        • First post
                          Last post
                        Buy SketchPlus
                        Buy SUbD
                        Buy WrapR
                        Buy eBook
                        Buy Modelur
                        Buy Vertex Tools
                        Buy SketchCuisine
                        Buy FormFonts

                        Advertisement