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

    Trace what prevents an object from being GC'd?

    Scheduled Pinned Locked Moved Developers' Forum
    22 Posts 4 Posters 907 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.
    • Dan RathbunD Offline
      Dan Rathbun
      last edited by

      It does occur to me that we do not have a call to tell the Sketchup C++ engine that we are done with a WebDialog instance and want it "disposed of".

      We would need an .dispose() instance method for the UI::WebDialog class, that would tell the C++ engine to get rid of it's reference (and then sets the receiver on the Ruby-side to nil [if that's possible.])

      Then you can get rid of your class definition using remove_const(:MyWebDialogSubclass)

      I'm not here much anymore.

      1 Reply Last reply Reply Quote 0
      • thomthomT Offline
        thomthom
        last edited by

        The thing is, a normal WebDialog instance will be GC'd if you clear all references to it.

        But there is something I have done in my subclass that prevents this - and I'm not finding out why.. 😞

        Thomas Thomassen — SketchUp Monkey & Coding addict
        List of my plugins and link to the CookieWare fund

        1 Reply Last reply Reply Quote 0
        • Dan RathbunD Offline
          Dan Rathbun
          last edited by

          @thomthom said:

          The thing is, a normal WebDialog instance will be GC'd if you clear all references to it.

          Sure.. as I remember Adam's post .. I thot he showed they were not GC'd.. but is was awhile back.

          It could be nothing more exotic than that Google did not account for us subclassing UI::WebDialog, similar to the way some API methods only accept exact classes and not any subclasses.

          I'm not here much anymore.

          1 Reply Last reply Reply Quote 0
          • Dan RathbunD Offline
            Dan Rathbun
            last edited by

            Did you dispose of all Locals, Constants, instance vars, and perhaps maybe even Procs that are within your subclass?

            I'm not here much anymore.

            1 Reply Last reply Reply Quote 0
            • thomthomT Offline
              thomthom
              last edited by

              Maybe... I was planning on trying a simple subclass - without all the stuff I have in mine. Just to see if that makes the differences.

              You could test the GC of a WebDialog if you like - so we can compare results.

              I did a count of the existing WebDialogs, then created a new one. Made a new count ( +2 in my results ) then I set my references to the WD to nil and invoke GC.start. The count then went down by 2. Confirm?

              Thomas Thomassen — SketchUp Monkey & Coding addict
              List of my plugins and link to the CookieWare fund

              1 Reply Last reply Reply Quote 0
              • thomthomT Offline
                thomthom
                last edited by

                @dan rathbun said:

                Did you dispose of all Locals, Constants, instance vars, and perhaps maybe even Procs that are within your subclass?

                I set all local variables to nil, nothing to the constants - just strings.

                Thomas Thomassen — SketchUp Monkey & Coding addict
                List of my plugins and link to the CookieWare fund

                1 Reply Last reply Reply Quote 0
                • Dan RathbunD Offline
                  Dan Rathbun
                  last edited by

                  @thomthom said:

                  You could test the GC of a WebDialog if you like - so we can compare results. ... Confirm?

                  OK, here:

                  dlg = UI;;WebDialog.new
                  >> #<UI;;WebDialog;0x61dc79c>
                  ObjectSpace.each_object(UI;;WebDialog) {}
                  >> 2
                  dlg=nil
                  >> nil
                  ObjectSpace.each_object(UI;;WebDialog) {}
                  >> 2
                  GC.start
                  >> nil
                  ObjectSpace.each_object(UI;;WebDialog) {}
                  >> 0
                  MyDialog = Class.new(UI;;WebDialog)
                  >> MyDialog
                  MyDialog.superclass
                  >> UI;;WebDialog
                  
                  
                  dlg=MyDialog.new
                  >> #<MyDialog;0x6071ed4>
                  ObjectSpace.each_object(UI;;WebDialog) {}
                  >> 2
                  ObjectSpace.each_object(MyDialog) {}
                  >> 2
                  dlg=nil
                  >> nil
                  ObjectSpace.each_object(MyDialog) {}
                  >> 2
                  ObjectSpace.each_object(UI;;WebDialog) {}
                  >> 2
                  GC.start
                  >> nil
                  ObjectSpace.each_object(MyDialog) {}
                  >> 0
                  ObjectSpace.each_object(UI;;WebDialog) {}
                  >> 0
                  
                  

                  I'm not here much anymore.

                  1 Reply Last reply Reply Quote 0
                  • Dan RathbunD Offline
                    Dan Rathbun
                    last edited by

                    But I did not show() the dialog in the previous test, so:

                    dlg=MyDialog.new('Test')
                    >> #<MyDialog;0x61d2f30>
                    dlg.show
                    >> true
                    # empty dialog with title 'Test' is displayed,
                    # .. and I close it manually.
                    ObjectSpace.each_object(UI;;WebDialog) {}
                    >> 2
                    ObjectSpace.each_object(MyDialog) {}
                    >> 2
                    dlg=nil
                    >> nil
                    ObjectSpace.each_object(MyDialog) {}
                    >> 2
                    ObjectSpace.each_object(UI;;WebDialog) {}
                    >> 2
                    GC.start
                    >> nil
                    ObjectSpace.each_object(UI;;WebDialog) {}
                    >> 0
                    ObjectSpace.each_object(MyDialog) {}
                    >> 0
                    

                    I'm not here much anymore.

                    1 Reply Last reply Reply Quote 0
                    • Dan RathbunD Offline
                      Dan Rathbun
                      last edited by

                      Are you testing on PC (v1.8.6-p287) or Mac (v1.8.5-p0) ??

                      I guess I should say that I'm running:
                      RUBY_VERSION 1.8.7 RUBY_PATCHLEVEL 299

                      .. get I set it for a test and forgot to set it back.

                      I'm not here much anymore.

                      1 Reply Last reply Reply Quote 0
                      • Dan RathbunD Offline
                        Dan Rathbun
                        last edited by

                        @ThomThom...

                        look at this example:
                        https://gist.github.com/355820

                        A the bottom he determines that there is a finalizer that is holding a ref to the instance, and therefor the reason why the instance is not getting GC'd.

                        I know you were trying to use a finalizer to cleanup a temp file.. likely that's the culprit.

                        I'm not here much anymore.

                        1 Reply Last reply Reply Quote 0
                        • thomthomT Offline
                          thomthom
                          last edited by

                          @dan rathbun said:

                          @ThomThom...

                          look at this example:
                          https://gist.github.com/355820

                          A the bottom he determines that there is a finalizer that is holding a ref to the instance, and therefor the reason why the instance is not getting GC'd.

                          I know you were trying to use a finalizer to cleanup a temp file.. likely that's the culprit.

                          Not in this case. I'm testing on SU8, default Ruby. I've used finalizer, but it only triggers under OSX. And I've read the gotchas about finalizers not GC'ing - and implemented it so it doesn't hold a reference.

                          However, I think I'm somehow creating a reference somewhere... I just can't track it down...

                          Thomas Thomassen — SketchUp Monkey & Coding addict
                          List of my plugins and link to the CookieWare fund

                          1 Reply Last reply Reply Quote 0
                          • thomthomT Offline
                            thomthom
                            last edited by

                            It seems to be due to using self.add_action_callback within the initialize method of the subclass.

                            <span class="syntaxdefault">class WC </span><span class="syntaxkeyword"><</span><span class="syntaxdefault"> UI</span><span class="syntaxkeyword">;;</span><span class="syntaxdefault">WebDialog</span><span class="syntaxkeyword">;</span><span class="syntaxdefault"> def initialize</span><span class="syntaxkeyword">(*</span><span class="syntaxdefault">args</span><span class="syntaxkeyword">);</span><span class="syntaxdefault"> super</span><span class="syntaxkeyword">;</span><span class="syntaxdefault"> self</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">add_action_callback</span><span class="syntaxkeyword">(</span><span class="syntaxstring">'test'</span><span class="syntaxkeyword">)</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">{</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">|</span><span class="syntaxdefault">diag</span><span class="syntaxkeyword">,</span><span class="syntaxdefault">param</span><span class="syntaxkeyword">|</span><span class="syntaxdefault"> puts diag</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> param </span><span class="syntaxkeyword">};</span><span class="syntaxdefault">end</span><span class="syntaxkeyword">;</span><span class="syntaxdefault">end</span><span class="syntaxkeyword">;<br /></span><span class="syntaxdefault">nil<br /><br />ObjectSpace</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">each_object</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">WC</span><span class="syntaxkeyword">){}<br /></span><span class="syntaxdefault">0<br /><br />x</span><span class="syntaxkeyword">=</span><span class="syntaxdefault">WC</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">new </span><span class="syntaxstring">'test'<br /></span><span class="syntaxcomment">#<WC;0x18dc1000><br /><br /></span><span class="syntaxdefault">ObjectSpace</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">each_object</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">WC</span><span class="syntaxkeyword">){}<br /></span><span class="syntaxdefault">2<br /><br />x</span><span class="syntaxkeyword">=</span><span class="syntaxdefault">nil<br />nil<br /><br />GC</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">start<br />nil<br /><br />ObjectSpace</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">each_object</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">WC</span><span class="syntaxkeyword">){}<br /></span><span class="syntaxdefault">2<br /></span>
                            

                            When I use add_action_callback outside initialize it does get GC'd.

                            <span class="syntaxdefault">ObjectSpace</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">each_object</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">UI</span><span class="syntaxkeyword">;;</span><span class="syntaxdefault">WebDialog</span><span class="syntaxkeyword">){}<br /></span><span class="syntaxdefault">2<br /><br />wd</span><span class="syntaxkeyword">=</span><span class="syntaxdefault">UI</span><span class="syntaxkeyword">;;</span><span class="syntaxdefault">WebDialog</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">new </span><span class="syntaxstring">'test'<br /></span><span class="syntaxcomment">#<UI;;WebDialog;0x18f1a2a8><br /><br /></span><span class="syntaxdefault">ObjectSpace</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">each_object</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">UI</span><span class="syntaxkeyword">;;</span><span class="syntaxdefault">WebDialog</span><span class="syntaxkeyword">){}<br /></span><span class="syntaxdefault">4<br /><br />wd</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">add_action_callback</span><span class="syntaxkeyword">(</span><span class="syntaxstring">'test'</span><span class="syntaxkeyword">)</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">{</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">|</span><span class="syntaxdefault">diag</span><span class="syntaxkeyword">,</span><span class="syntaxdefault">param</span><span class="syntaxkeyword">|</span><span class="syntaxdefault"> puts diag</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> param </span><span class="syntaxkeyword">}<br /></span><span class="syntaxdefault">true<br /><br />wd </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> nil<br />nil<br /><br />GC</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">start<br />nil<br /><br />ObjectSpace</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">each_object</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">UI</span><span class="syntaxkeyword">;;</span><span class="syntaxdefault">WebDialog</span><span class="syntaxkeyword">){}<br /></span><span class="syntaxdefault">2<br /></span>
                            

                            I don't understand why...

                            Thomas Thomassen — SketchUp Monkey & Coding addict
                            List of my plugins and link to the CookieWare fund

                            1 Reply Last reply Reply Quote 0
                            • Dan RathbunD Offline
                              Dan Rathbun
                              last edited by

                              @thomthom said:

                              It seems to be due to using self.add_action_callback within the initialize method of the subclass.

                              I don't understand why...

                              Oh.. that's an easy one: Because **self** IS a reference to THE instance.
                              So YOUR telling Ruby to keep 'literal' reference within the code.

                              You dont need that reference by the way.. because the initialize method ALWAYS gets executed WITHIN the newly created instance, just after the class constructor (usually new,) creates it.

                              So just remove the " self." from in front of ALL instance method calls made from within ANY of the object's instance methods.

                              I'm not here much anymore.

                              1 Reply Last reply Reply Quote 0
                              • Chris FullmerC Offline
                                Chris Fullmer
                                last edited by

                                Interesting, 👍

                                Lately you've been tan, suspicious for the winter.
                                All my Plugins I've written

                                1 Reply Last reply Reply Quote 0
                                • thomthomT Offline
                                  thomthom
                                  last edited by

                                  I had a suspicion that might be it, and it's what I had planned to try next. But I still don't understand why. As self.add_action_callback is just calling a method, never is self being stored in any reference... ❓

                                  Thomas Thomassen — SketchUp Monkey & Coding addict
                                  List of my plugins and link to the CookieWare fund

                                  1 Reply Last reply Reply Quote 0
                                  • thomthomT Offline
                                    thomthom
                                    last edited by

                                    Removing self. from add_action_callback made no difference.

                                    Thomas Thomassen — SketchUp Monkey & Coding addict
                                    List of my plugins and link to the CookieWare fund

                                    1 Reply Last reply Reply Quote 0
                                    • thomthomT Offline
                                      thomthom
                                      last edited by

                                      I finally nailed it. I created the procs for add_action_callback as class constants instead of creating blocks within the initialize method and that allowed the WebDialog to be GC'd.

                                      Thomas Thomassen — SketchUp Monkey & Coding addict
                                      List of my plugins and link to the CookieWare fund

                                      1 Reply Last reply Reply Quote 0
                                      • Dan RathbunD Offline
                                        Dan Rathbun
                                        last edited by

                                        @unknownuser said:

                                        (http://forums.sketchucation.com/viewtopic.php?f) of this topic":25oe0j3o]

                                        Did you dispose of all Locals, Constants, instance vars, and perhaps maybe even Procs that are within your subclass?

                                        That said... you should be able to have proc refs, if they are instance @refs because they are likely to have refs inside them that ref instance objects.

                                        If the class is a singleton (most WebDialogs are,) just limit the number of instances to 1, in the wrapping namespace, the same way you would for a tool instance.

                                        I'm not here much anymore.

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

                                        Advertisement