From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-0.3 required=5.0 tests=BAYES_00, REPLYTO_WITHOUT_TO_CC autolearn=no autolearn_force=no version=3.4.4 Path: border2.nntp.dca1.giganews.com!nntp.giganews.com!usenet.blueworldhosting.com!feeder01.blueworldhosting.com!feeder.erje.net!eu.feeder.erje.net!news.etla.org!aioe.org!.POSTED!not-for-mail From: "Dmitry A. Kazakov" Newsgroups: comp.lang.ada Subject: Re: Should weak counted reference be able to be strengthened? Date: Fri, 21 Nov 2014 14:16:01 +0100 Organization: cbb software GmbH Message-ID: <7d9r3whe43z8$.1f2l3n5cz0zh0$.dlg@40tude.net> References: Reply-To: mailbox@dmitry-kazakov.de NNTP-Posting-Host: nyHeW7QjJmC1odUjK4LkDA.user.speranza.aioe.org Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit X-Complaints-To: abuse@aioe.org User-Agent: 40tude_Dialog/2.0.15.1 X-Notice: Filtered by postfilter v. 0.8.2 Xref: number.nntp.giganews.com comp.lang.ada:190909 Date: 2014-11-21T14:16:01+01:00 List-Id: On Fri, 21 Nov 2014 11:07:07 +0000 (UTC), Natasha Kerensikova wrote: > So my question is, now that I am considering extending my > reference-counting system to include weak reference, do I really needed > a public interface to build a strong reference from a weak one? Yes. > Or is it enough to have weak reference primitives that return Accessor > and Mutator objects, thereby allowing safe access without allowing a > weak reference to be promoted into a strong one? How this is supposed to work? The only safe referential object is a strong reference. > In all the use cases I can imagine, the weak reference is only used to > access the referred object. I can't make up an example where a strong > reference would be needed from only a weak one. Am I missing something? Yes. There are many cases when weak references are components of containers or when weak references are used in composite objects which change their content upon disappearance of the referenced object. > The interesting point I see in forbidding such a "reference > strengthening" is to prevent some cases circular strong references, > which is the problem weak references are supposed to solve. When a weak reference is promoted to a strong one that occurs on a nested context. Such strong references are allocated on the stack. Therefore there is no any danger of getting circular dependencies unless the structure of references in objects is already circular. In short, there is nothing to worry about. > As a side note, I'm a bit surprised by how easy weak/strong reference > seem to be: I think I would only need to store two counters instead of > one, one for the strong references and one for all references, > deallocating the object when the strong reference counter reaches > zero, and freeing the counters when the total counter reaches zero -- as > if each weak reference was a strong reference to the counters. Then it's > only a matter of adding an atomic increase-if-not-zero to the strong > reference counter to build accessors from weak references. > Is there a subtle trap I'm missing? Weak references usually require a notification mechanism when the object disappears. Thus you need a count and the head of the doubly-linked list of the weak references pointing to the object. In some complicated design you may need an identification of the holder of a read-write strong reference. That is when referenced objects are mutated while accessed in an immutable way in parallel. This is usually the case when manipulating structures like graphs and trees. A read-write reference clones the object if this is the first reference and works with already existing copy if this is the second reference from the same context. -- Regards, Dmitry A. Kazakov http://www.dmitry-kazakov.de