{"id":542,"date":"2020-01-01T09:54:00","date_gmt":"2020-01-01T16:54:00","guid":{"rendered":"https:\/\/www.great-white-software.com\/blog\/?p=542"},"modified":"2019-12-31T15:55:17","modified_gmt":"2019-12-31T22:55:17","slug":"weakref-usage","status":"publish","type":"post","link":"https:\/\/www.great-white-software.com\/blog\/2020\/01\/01\/weakref-usage\/","title":{"rendered":"WeakRef usage"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">There was a good question from Markus in a recent comment on a different post of mine.<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>In children of a window we should keep track of the parent window by using WeakRef.<br>But what if I have a class cDepartment with a list of employees defined as Employees() as cEmployee \u2013 should that then better also be defined as WeakRef? And why or why not?<\/p><\/blockquote>\n\n\n\n<p class=\"wp-block-paragraph\">First I&#8217;ll say &#8220;it depends&#8221; as there&#8217;s certainly no single correct answer to such a question. In many cases it will depend on the expected usage.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">WeakRefs are a curious thing. In Xojo most times you have a reference to an object its a strong reference. What happens is that the objects reference count is increased and only when that counter gets set to 0 will the object be destroyed.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For instance, if we have code like the following <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Class Foo\n\n  Sub Destructor()\n    break\n  End Sub\n\nEnd Class\n\n\ndim firstRef as Foo\ndim secondRef as Foo\n\nfirstRef = new Foo \n\/\/ the object firstRef refers to now has a reference count of 1\n\nsecondRef = firstRef \n\/\/ since secondRef refers to the SAME object as firstRef\n\/\/ the object firstRef refers to now has a reference count of 2\n\nfirstRef = nil\n\/\/ the object firstRef refers to now has a reference count of 1\n\nsecondRef = nil\n\/\/ the object firstRef refers to now has a reference count of 0\n\/\/ and it will be destroyed and the breakpoint in the destructor\n\/\/ will be encountered\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Now what happens if we use a WeakRef instead for <em>secondRef<\/em> ? If we turn that code into the following <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Class Foo\n\n  Sub Destructor()\n    break\n  End Sub\n\nEnd Class\n\n\nDim firstRef As Foo\ndim secondRef as Foo\nDim weakSecondRef As WeakRef \/\/ &lt;&lt;&lt;&lt;&lt;&lt;&lt; note the type !\n\nfirstRef = New Foo \n\/\/ the object firstRef refers to now has a reference count of 1\n\nweakSecondRef = New WeakRef(firstRef)\n\/\/ since weakSecondRef is a WeakRef the object \n\/\/ firstRef refers does NOT increment its reference count\n\nfirstRef = Nil\n\/\/ the object firstRef refers to now has a reference count of 0\n\/\/ and it will be destroyed and the breakpoint in the destructor\n\/\/ will be encountered now\n\n\/\/ but what about weakSecondRef's contents ???\n\/\/ lets fetch that value and see whats in there now\nsecondRef = Foo(weakSecondRef.Value)\n\nbreak \/\/ &lt;&lt;&lt; here you will find that the returned value is NIL !<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">And if you try to use the object contained by the weakref secondRef you will find that it is nil. A weakref allows an object to be referenced in a way that it does not increment the original objects reference count which CAN let that object be nil&#8217;d in other code.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">When you use a weakref you MUST check the VALUE of the weakref to see if what you get back is NIL because the object may have been set to nil elsewhere.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Now that we have the basics of weakrefs out of the way lets turn back to Markus&#8217; original and my original reply of &#8220;it depends&#8221;.<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p><em>n children of a window we should keep track of the parent window by using WeakRef.<\/em><\/p><\/blockquote>\n\n\n\n<p class=\"wp-block-paragraph\">Indeed you <em>may<\/em> want to have child windows retain a weakref to refer to a parent window. It would allow you to close the parent and not the child, as the weakref will not increment the reference count of the parent window. But, you <em>may<\/em> want the parent to not be set to nil when there are child windows.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This decision will depend entirely on how your UI is supposed to work. If those child windows should not exist without a parent then a hard reference to the parent may make perfect sense. <\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p><em>But what if I have a class cDepartment with a list of employees defined as Employees() as cEmployee \u2013 should that then better also be defined as WeakRef? And why or why not?<\/em><\/p><\/blockquote>\n\n\n\n<p class=\"wp-block-paragraph\">Again this will depend entirely on how the <em>cDepartment<\/em> class should behave. Can <em>cEmployees<\/em> be added &amp; removed at runtime and should the <em>cDepartment<\/em> class be able to deal with this ? Or should <em>cDepartment<\/em> need to be reconstituted when such changes of its employees are made ? The correct behaviour isnt dictated by the underlying code and whether you should use weakrefs but by the needs of the application and how <em>cDepartment<\/em> should behave.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You could always use weakrefs but this could have unintended consequences for the <em>cDepartment<\/em> class as this would allows its <em>cEmployees<\/em> to be removed at runtime. If you had used a FOR EACH loop with an iterator to perform some function you would need to handle the exceptions that could be encountered. Iterators will be  invalidated by removal of an employee while iterating over the list and will raise an exception of this occurs. Weakrefs would permit this. Hard references would not.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>There was a good question from Markus in a recent comment on a different post of mine. In children of a window we should keep track of the parent window by using WeakRef.But what if I have a class cDepartment with a list of employees defined as Employees() as cEmployee \u2013 should that then better &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.great-white-software.com\/blog\/2020\/01\/01\/weakref-usage\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;WeakRef usage&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_post_was_ever_published":false},"categories":[1],"tags":[],"class_list":["post-542","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/posts\/542","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/comments?post=542"}],"version-history":[{"count":3,"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/posts\/542\/revisions"}],"predecessor-version":[{"id":547,"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/posts\/542\/revisions\/547"}],"wp:attachment":[{"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/media?parent=542"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/categories?post=542"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/tags?post=542"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}