About WeakRefs

  1. ‹ Older
  2. 4 months ago

    Kem T

    Jun 10 Pre-Release Testers, Xojo Pro, XDC Speakers Connecticut

    So when might you want a WeakRef instead of merely assigning the object to another variable?

    The most common use is to prevent a circular references, that is, an object that holds a second object where the second object holds the first object. With a circular reference, you can lose access to an object that will never be destroyed and create a memory leak.

    The most common scenario here is the parent/child relationship. Lets say you create a Tree class that can hold many Leaf classes in an array. From any Leaf, you want to be able to get back to its Tree. That is, the Tree is the parent and the many Leaf's are its children. Consider this simple example where a Tree has a single Leaf:

    dim t as new Tree
    dim l as new Leaf
    
    t.Child = l
    l.Parent = t
    
    t = Nil
    l = Nil

    Even though we've set both variables to Nil, both still exist because each refers to the other. To prevent that, you'd have to change the code that gets rid of the objects to something like this instead:

    t.Child = Nil
    t = Nil // The Tree object sill exists because l.Parent refers to it
    l = Nil // No more references to the Leaf, and since it's gone, no more to the Tree either

    But there is an easier way. Instead of defining Leaf.Parent as a Tree, define it as a WeakRef instead.

    dim t as new Tree
    dim l as new Leaf
    
    t.Child = l
    l.Parent = new WeakRef( t )
    
    l = Nil // The Leaf still exists because t.Child refers to it
    t = Nil // Since t was the only reference, Tree goes away and takes Leaf with it

    A Computed Property in Leaf that handles the WeakRef mechanics makes the process painless. But either way, since the only "true" reference is of the Parent to its Child, the problem of the Circular Reference evaporates.

    In the next post, I'll offer a different example.

  3. Kem T

    Jun 10 Pre-Release Testers, Xojo Pro, XDC Speakers Connecticut

    Imagine code that sends objects through queues. While an object is waiting to be processed something might happen that renders it invalid or unnecessary. How to handle that?

    For example, suppose there is a UI where the user can click off multiple items where each item represents an object that is placed on a queue for processing. After the user has ticked off multiple objects, they change their mind and uncheck a few. How to take them off the queue?

    You could search the queue for that object and delete it. But what if your code uses multiple queues? What it you might add more in the future? This can all be solved easily with a WeakRef.

    The main array holds your objects and each queue holds a WeakRef to that object. When it comes time to process an item in the queue, the code resolves it back to its original object. If it can't, that is, the WeakRef evaluates to Nil because there are no other references to it, the queue processor ignores it and moves on. In this scenario, you can remove the object from every queue at once by simply deleting it from the main array. Bang, no more references to the object, so no more object, and the queues simultaneously have empty placeholders in the slots that originally represented that object.

    ---------

    I hope someone finds this helpful. If you have other real-world examples, please post them.

  4. Sam R

    Jun 10 Pre-Release Testers, Xojo Pro, Third Party Store Hengchun, Pingtung, Taiwan

    I use WeakRefs to ovoid circular relationships between classes.

    A "Parent" object uses strong references to hold on to it's "Children".

    A "Child" object uses a WeakRef for it's relationship to it's parent.

  5. Kem T

    Jun 10 Pre-Release Testers, Xojo Pro, XDC Speakers Connecticut

    I think you scrolled past my second post. :)

  6. Garry P

    Jun 11 Pre-Release Testers, Xojo Pro Europe (Torquay, UK)

    Great post Kem. Switching to WeafRefs for the parents in my tree structure in the Markdown parser I’m working on right now.

  7. Wayne G

    Jun 11 Pre-Release Testers, Xojo Pro New Zealand axisdirect.nz

    Excellent stuff @Kem T, It's a shame there's no knowledge base to keep this current. Perhaps @Xojo U might link to this conversation from the docs.

  8. Bob K

    Jun 11 Pre-Release Testers, Xojo Pro, Third Party Store Kansas City

    Awesome post, Kem!

  9. brian f

    Jun 11 Pre-Release Testers, Xojo Pro Chilly California

    Kem,

    post was kinda weak ;)

    Thanks for the information!

  10. Kem T

    Jun 11 Pre-Release Testers, Xojo Pro, XDC Speakers Connecticut

    Along with "like", we need a "slap" option.

    :P

  11. Joost R

    Jun 11 Pre-Release Testers, Xojo Pro The Netherlands

    @Wayne G Excellent stuff @Kem T, It's a shame there's no knowledge base to keep this current. Perhaps @Xojo U might link to this conversation from the docs.

    Yes, this kind of very useful education should be kept somewhere. For now, I capture Kem's 'masterclass' in my personal KB.
    @Kem T I encourage you to join @Paul L for some great webinars.

  12. Kem T

    Jun 11 Pre-Release Testers, Xojo Pro, XDC Speakers Connecticut

    @Paul L has turned this post into part of the online User Guide. See:

    https://docs.xojo.com/UserGuide:Weak_References

  13. Norman P

    Jun 11 Pre-Release Testers, Xojo Pro great-white-software.com/blog
    Edited 4 months ago

    yeah tis all wrong anyways

    j/k :P

  14. Kem T

    Jun 11 Pre-Release Testers, Xojo Pro, XDC Speakers Connecticut

    *slap*

  15. Norman P

    Jun 11 Pre-Release Testers, Xojo Pro great-white-software.com/blog

    fine - back to my cave then

  16. Dave S

    Jun 11 San Diego, California USA

    @brian f post was kinda weak

    but it could be a great reference

  17. Kem T

    Jun 11 Pre-Release Testers, Xojo Pro, XDC Speakers Connecticut

    *SLAP*!!

  18. Dave S

    Jun 11 San Diego, California USA
    Edited 4 months ago

    OW !!!

  19. Norman P

    Jun 11 Pre-Release Testers, Xojo Pro great-white-software.com/blog
    Edited 4 months ago

    I'd liken it to you being out with parents & kids
    if you hang on to each of them by their shirts they cant go anywhere as long as you hold on
    and maybe several parents are hanging on to the same kids so if you let go the kid still cant run off

    but if you hang onto a kid who you entrust to hang onto another kid and they let go of that kid and no one else is hanging on to them well .... that other kid could be long gone
    you might still have a hold of the first kid (the weakref) but what it holds onto is gone
    and you might not know until its too late

    that said this should also spur some notes about AddressOf and WeakAddressOf as they are VERY similar - except with WeakAddressOf you have NO way to even tell if the ultimate target is gone - it just can be and your app dies

  20. Brian O

    Jun 12 Pre-Release Testers, Xojo Pro Calgary, AB

    i think i ran into comprehension issues with weakref and WeakAddressOf when setting up callbacks for embedded objects.

  21. Norman P

    Jun 12 Pre-Release Testers, Xojo Pro great-white-software.com/blog

    weakaddressof is problematic as you cannot tell if th target of the callback has gone away
    timer.calllater has much the same issue

or Sign Up to reply!