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=-1.9 required=5.0 tests=BAYES_00 autolearn=unavailable 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!newsfeed.fsmpi.rwth-aachen.de!eternal-september.org!feeder.eternal-september.org!mx02.eternal-september.org!.POSTED!not-for-mail From: Natasha Kerensikova Newsgroups: comp.lang.ada Subject: Should weak counted reference be able to be strengthened? Date: Fri, 21 Nov 2014 11:07:07 +0000 (UTC) Organization: A noiseless patient Spider Message-ID: Injection-Date: Fri, 21 Nov 2014 11:07:07 +0000 (UTC) Injection-Info: mx02.eternal-september.org; posting-host="76a49b86bc3e16725b7cfca3d85cb4c8"; logging-data="20461"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/4Tms2pTv/lIa9MwwCrFR9" User-Agent: slrn/1.0.1 (FreeBSD) Cancel-Lock: sha1:gRLI0uHXanBxprYJxgv2HFKnoxs= Xref: number.nntp.giganews.com comp.lang.ada:190905 Date: 2014-11-21T11:07:07+00:00 List-Id: Hello, I hope I'm not too off-topic, but I have a question about designing weak and strong references in a reference-counting system, implemented in Ada. In most of the general discussion about weak references, the title question is a no-brainer: since the object referred by a weak reference can disappear at any time, a strong reference must be obtained from the weak reference before actually accessing the object. However, in my (strong-only) current reference counting system, and in most of those written in Ada, the referred object is not accessed directly from the reference, instead they use an access discriminant in a Mutator object (or an access constant discriminant in an Accessor), and the object contains privately a strong reference, because in some weird situations the reference might disappear before the mutator. 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? 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? 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? 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. Indeed, when an object has only a weak reference, it means it's dangerous for it to hold a strong one, so it might also be dangerous for it to hand one over to anything it can access. The holder can still do bad things by storing an Accessor or a Mutator object, but it is much more obviously a bad thing. What are your thoughts on this? 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? Thanks in advance for your help, Natasha