From: Jeremiah <jeremiah.breeden@gmail.com>
Subject: Trying to understand Ada.Finalization.Controlled assignment mechanics.
Date: Mon, 22 Sep 2014 17:43:37 -0700 (PDT)
Date: 2014-09-22T17:43:37-07:00 [thread overview]
Message-ID: <024b1649-e056-4b7a-9072-7c7ef0c53f0b@googlegroups.com> (raw)
My understanding of assignment of a child of Ada.Finalization.Controlled is that if you do the following:
A := B; -- A and B are derived from Ada.Finalization.Controlled
That the following occurs:
Finalize(A);
Copy B into A;
Adjust(B);
Is this correct?
I've been learning/relearning Ada lately, so I have been mainly making programs to get a better understanding of the mechanics of Ada. In order to understand assignment, I made a quick tester class. It isn't very useful nor is it meant to be, but it highlights something that I don't understand.
test_class2.ads
----------------------------------
with Ada.Finalization;
package test_class2 is
type access_type is access Integer;
type test is new Ada.Finalization.Controlled with record
ref : access_type := null;
end record;
function Make(a : access_type) return test;
overriding procedure Adjust(self : in out test);
overriding procedure Finalize(self : in out test);
end test_class2;
test_class2.adb
--------------------------
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Unchecked_Deallocation;
package body test_class2 is
function Make(a : access_type) return test is
begin
return (Ada.Finalization.Controlled with ref => a);
end Make;
overriding procedure Adjust(self : in out test) is
begin
Put_Line("Adjusting");
self.ref := null;
end Adjust;
overriding procedure Finalize(self : in out test) is
procedure Free is new Ada.Unchecked_Deallocation(Integer, access_type);
begin
Put("Finalizing");
if(self.ref /= null) then
Put(" (FREED)");
Free(self.ref);
end if;
New_Line;
end Finalize;
end test_class2;
Main.adb
--------------------------
with Ada.Text_IO; use Ada.Text_IO;
with test_class2;
procedure Main is
tester : test_class2.test := test_class2.Make(new Integer'(45));
begin
Put_Line("Hello World");
end Main;
The output I am seeing doesn't make sense to me:
Adjusting
Finalizing (FREED)
Adjusting
Finalizing
Hello World
Finalizing
I wouldn't expect the Freeing of memory to happen until after Hello World when tester goes out of scope. I also thought that Finalize happens before Adjust, but Adjust looks like is happening first. I am compiling and running this on GNAT GPL for windows if that makes a difference. I don't have a different hardware platform to test on.
Can anyone explain to me why the "freeing" of memory happens prior to Hello World and whey Adjusting happens before Finalization? I figure I have something fundamental that I am missing.
Thanks!
next reply other threads:[~2014-09-23 0:43 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2014-09-23 0:43 Jeremiah [this message]
2014-09-23 1:17 ` Trying to understand Ada.Finalization.Controlled assignment mechanics Jeffrey Carter
2014-09-23 16:08 ` Jeremiah
2014-09-23 16:23 ` Adam Beneschan
2014-09-23 17:39 ` Simon Wright
2014-09-23 17:50 ` Jeffrey Carter
2014-09-23 19:19 ` Robert A Duff
2014-09-23 21:59 ` Jeremiah
2014-09-24 10:59 ` AdaMagica
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox