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.1 required=5.0 tests=BAYES_00, PP_MIME_FAKE_ASCII_TEXT autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII X-Google-Thread: 103376,f196003f7b36852b X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2001-04-24 20:14:30 PST Path: newsfeed.google.com!newsfeed.stanford.edu!paloalto-snf1.gtei.net!news.gtei.net!enews.sgi.com!newshub2.rdc1.sfba.home.com!news.home.com!news1.sttls1.wa.home.com.POSTED!not-for-mail From: "DuckE" Newsgroups: comp.lang.ada References: Subject: Re: Question about Finalization Control and reference counting X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 5.50.4133.2400 X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4133.2400 Message-ID: Date: Wed, 25 Apr 2001 03:14:29 GMT NNTP-Posting-Host: 24.6.221.63 X-Complaints-To: abuse@home.net X-Trace: news1.sttls1.wa.home.com 988168469 24.6.221.63 (Tue, 24 Apr 2001 20:14:29 PDT) NNTP-Posting-Date: Tue, 24 Apr 2001 20:14:29 PDT Organization: Excite@Home - The Leader in Broadband http://home.com/faster Xref: newsfeed.google.com comp.lang.ada:6908 Date: 2001-04-25T03:14:29+00:00 List-Id: Try placing the access variable inside the protected type: package Managed_Allocation is type Node is private; function New_Node return Node; private type Node_Data is record Reference_Count : Natural := 0; end record; type Node_Data_Access is access all Node_Data; type Node is new Ada.Finalization.Controlled with record Node_Data_Ref : Node_Data_Access; end record; procedure Initialize ( Object : in out Node ); procedure Adjust ( Object : in out Node ); procedure Finalize ( Object : in out Node ); end Managed_Allocation; with Ada.Unchecked_Deallocation; package body Managed_Allocation is function New_Node return Node is Data_Node : Node; begin Data_Node.Node_Data_Ref := new Node_Data; return Data_Node; end New_Node; procedure Free is new Ada.Unchecked_Deallocation( Node_Data, Node_Data_Access ); procedure Initialize ( Object : in out Node ) is begin Object .Node_Data_Ref := null; end Initialize; procedure Adjust ( Object : in out Node ) is begin if Object .Node_Data_Ref /= null then Object.Node_Data_Ref.Reference_Count := Object .Node_Data_Ref. Reference_Count + 1; end if; end Adjust; procedure Finalize ( Object : in out Node ) is begin if Object.Node_Data_Ref /= null then Object.Node_Data_Ref.Reference_Count := Object.Node_Data_Ref. Reference_Count - 1; if Object .Node_Data_Ref.Reference_Count = 0 then Free( Object .Node_Data_Ref ); Object .Node_Data_Ref := null; end if; end if; end Finalize; end Managed_Allocation; BTW: I didn't check, but you can probably find another example of this at: www.adapower.com SteveD "Preben Randhol" wrote in message news:slrn9e2vqb.aah.randhol+abuse@kiuk0156.chembio.ntnu.no... > > Hi > > I'm wondering if it is possible to do something like this > > type Data_Type is new Ada.Finalization.Controlled with > record > References: Integer := 0; > end record; > > type Data_Access_Type is access Data_Type; > > [..] > > and then in the main program if I do something like this: > > procedure main is > > Pointer : Data_Access_Type := null; > Data : Data_Access_Type := new Data_Type; > > begin > > Pointer := Data; > > end main; > > to have Data.References increases by using Adjust? I'm trying but it > looks like it only works if I do: > > Pointer : Data_Type; > ... > Pointer := Data.all; > ... > otherwise Adjust is not invoked. Is it possible to get it invoked if one > point an access type to an object? I'm trying to eliminate the problem > of dangling pointers in a program... > > Thanks in advance. > > -- > Preben Randhol ------------------- http://www.pvv.org/~randhol/ -- > �For me, Ada95 puts back the joy in programming.�