Circular Reference and Weak Property Pattern

  1. 4 months ago
    Edited 4 months ago by Mark S

    I have a class, MyObjectClass with a property, MyReport of type MyReportClass.
    I have a Report class called, MyReportClass with a property, MyObject of type MyObjectClass.

    MyObjectClass has a method called MakeReport:

      If MyReport = Nil then
        
        MyReport = New MyReportClass(me)
        
      End if

    This gives me a circular reference.

    MyObjectClass is stored in the RowTag of a listbox.

    1) If rows in the listbox never get removed for the life of the app, do I need to be concerned about the circular reference?

    2) Assuming I do need to remove rows, I've been reading about using the Weak Property Pattern, but I don't understand how this works. Could someone explain please?
    http://www.mbsplugins.de/archive/2014-08-26/Tip_of_the_day_Weak_Property_p/monkeybreadsoftware_blog_archive

    Thank you

  2. Kem T

    Dec 13 Pre-Release Testers, Xojo Pro Answer New York

    From some years ago but still relevant.

    http://www.monkeybreadsoftware.eu/listarchive-realbasic-nug/2009-12-12-2.shtml

  3. 3 months ago

    Javier M

    Dec 13 Pre-Release Testers, Xojo Pro Madrid, Spain

    Hi Mark,

    Maybe you can also find of interest this entry in the Xojo Blog: http://blog.xojo.com/2016/04/20/weakref-and-memory-management/

    Javier

  4. Thanks guys. The wording of the first article I posted, tripped me up on typing the properties. Specifically, "The private property created by Xojo now changes type to WeakRef." The private property needs to be changed manually and the original property stays as declared.

  5. Kem T

    Dec 14 Pre-Release Testers, Xojo Pro New York

    One suggestion that wasn't in my original post: use computed properties to manage the WeakRefs for you.

    • Create a computed property, MyParent As ParentClass
    • Create a shadow property, mMyParentWR As WeakRef
    • Use the MyParent getter and setter to translate to and from the WeakRef.

    In this way, you can just reference MyParent without worrying about the accompanying processing. Just be prepared for MyParent to be nil.

    Get
      if mMyParentWR is nil then
        return nil
      else
        return ParentClass( mMyParentWR.Value )
      end if
    
    Set
      if value is nil then
        mMyParentWR = nil
      else
        mMyParentWR = new WeakRef( value )
      end if
  6. Tim H

    Dec 14 Pre-Release Testers Portland, OR USA

    That's what Christian's post does (that he referred to in the OP), but it contained an error and was thus misleading.

  7. 6 weeks ago

    Dan B

    Feb 17 Pre-Release Testers, Xojo Pro

    ^This helped me a lot today, thanks all!

or Sign Up to reply!