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=ham autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,f196003f7b36852b X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2001-04-25 15:43:12 PST Newsgroups: comp.lang.ada Path: newsfeed.google.com!newsfeed.stanford.edu!skynet.be!newsfeed.online.be!zur.uu.net!ash.uu.net!xyzzy!nntp From: Jeffrey Carter Subject: Re: Question about Finalization Control and reference counting X-Nntp-Posting-Host: e246420.msc.az.boeing.com Content-Type: text/plain; charset=us-ascii Message-ID: <3AE74E17.FD004E37@boeing.com> Sender: nntp@news.boeing.com (Boeing NNTP News Access) Content-Transfer-Encoding: 7bit Organization: The Boeing Company X-Accept-Language: en References: <3AE5A5B5.4337CE90@boeing.com> Mime-Version: 1.0 Date: Wed, 25 Apr 2001 22:22:15 GMT X-Mailer: Mozilla 4.5 [en]C-CCK-MCD Boeing Kit (WinNT; U) Xref: newsfeed.google.com comp.lang.ada:6940 Date: 2001-04-25T22:22:15+00:00 List-Id: Preben Randhol wrote: > > On Tue, 24 Apr 2001 16:11:33 GMT, Jeffrey Carter wrote: > > Adjust only applies to a controlled type, so you need to put your access > > value inside a controlled type: > > > > type Node; > > > > type Node_Ptr is access all Node; > > > > type Node is new [Limited_]Controlled with record > > Prev : Node_Ptr; > > Data : Whatever; > > Count : Natural := 0; > > Next : Node_Ptr; > > end record; > > > > procedure Adjust (Object : in out Node); > > > > procedure Finalize (Object : in out Node); > > But the problem is that if you have : > > Node1 : Node_Ptr; > Node2 : Node_Ptr; > Node3 : Node; > > ... > > After making the Nodes with new you: > > Node1.Next := Node2; > > Then Adjust of Node2 won't be called it will only be called if you > do: > > Node3 := Node1.all; > > if I understand things correctly. I think I need to rethink my > problem in a more Ada approach :-) Perhaps I'm trying to be too user friendly. Or maybe I'm failing. For a real list component, Node would not be controlled, but the list type would. A list component will generally not make its access type visible to its clients. All modification of a list will be through the operations provided, so freeing space when a node is deleted can be handled properly through those operations. You would rather want to define Adjust and Finalize for an entire List, to ensure that memory is managed properly. If you do really want to control individual nodes, then you need to replace the access type with a "smart pointer", an access value inside a controlled type that points to an object with a reference count. Adjust would increment the count, and Finalize would decrement it and deallocate the object when it reaches zero. Then Node1.Next := Node2; will Finalize Node1 and Adjust its new value. However, to access the node directly, you have to say Node.Value.all. -- Jeff Carter