sketchucation logo sketchucation
    • Login
    Oops, your profile's looking a bit empty! To help us tailor your experience, please fill in key details like your SketchUp version, skill level, operating system, and more. Update and save your info on your profile page today!
    FredoBend | Powerful new bending tool for SketchUp Download

    Observers not garbage collected?

    Scheduled Pinned Locked Moved Developers' Forum
    3 Posts 2 Posters 337 Views 2 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.
    • thomthomT Offline
      thomthom
      last edited by

      Create an observer class...
      class ObTest < Sketchup::ModelObserver; def onTransactionUndo(model); puts 'onTransactionUndo'; end; end; nil model=Sketchup.active_model #<Sketchup::Model:0xf584d10>

      Create an model observer and test it...
      x = ObTest.new #<ObTest:0xf4f5568> model.add_observer( x ) true onTransactionUndo onTransactionUndo onTransactionUndo

      Check the instance count for the observer...
      ObjectSpace.each_object(ObTest){} 1
      As expected.

      Remove the observer...
      model.remove_observer( x ) true ObjectSpace.each_object(ObTest){} 1
      Still one - but we have not triggered the GC yet.

      Trigger the GC...
      GC.start nil ObjectSpace.each_object(ObTest){} 1
      Still there! This is not expected.

      Set our instance reference to nil...
      x=nil nil GC.start nil ObjectSpace.each_object(ObTest){} 1
      Still there!!

      Any ideas?
      Bug?

      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

        Yes a bug in a way... the call to add_observer creates a C++ reference to the object that is not released by the remove_observer call.

        So ..Ruby GC cannot dispose of the object.
        In the test code below, you'll see the object is GC'd when the add_observer and remove_observer calls are commented out.

        OT.rb

        I'm not here much anymore.

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

          This is somewhat troublesome.
          One would then have to be very careful when creating observer instances. Ideally only create one instance - ever, if possible.
          And also clean up any references the observer has to other objects when possible, as otherwise they won't be GC'd either.

          grumble refactoring time.

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

          1 Reply Last reply Reply Quote 0

          Hello! It looks like you're interested in this conversation, but you don't have an account yet.

          Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

          With your input, this post could be even better πŸ’—

          Register Login
          • 1 / 1
          • First post
            Last post
          Buy SketchPlus
          Buy SUbD
          Buy WrapR
          Buy eBook
          Buy Modelur
          Buy Vertex Tools
          Buy SketchCuisine
          Buy FormFonts

          Advertisement