2
A
답변
5
Ada.Finalization.Controlled 또는 Limited_Controlled에서 파생 된 하나 이상의 레코드에서 파이널 라이즈에 참여한 개인 멤버를 래핑합니다. 보호 오브젝트가 마무리되면 해당 개인 회원도 이에 따라 완결됩니다. (!) :
with Text_IO; use Text_IO;
with Ada.Finalization;
with Ada.Containers.Ordered_Maps;
with Ada.Unchecked_Deallocation;
procedure Protected_Final is
Instance_Counter : Natural := 1;
package Int_Map is new Ada.Containers.Ordered_Maps (Integer, Integer);
subtype Map is Int_Map.Map;
type Map_Wrapper is new Ada.Finalization.Controlled with record
ID : Natural;
M : Map;
end record;
overriding procedure Initialize(Item : in out Map_Wrapper);
overriding procedure Finalize(Item : in out Map_Wrapper);
procedure Initialize(Item : in out Map_Wrapper) is
begin
Item.ID := Instance_Counter;
Instance_Counter := Instance_Counter + 1;
Put_Line("Initialize the Map part as instance" & Natural'Image(Item.ID));
end Initialize;
procedure Finalize(Item : in out Map_Wrapper) is
begin
Put_Line("Clean up the Map stuff for instance" & Natural'Image(Item.ID));
end Finalize;
protected type A is
procedure Foo;
private
M : Map_Wrapper;
end A;
protected body A is
procedure Foo is
begin
null;
end Foo;
end A;
Test_Item : A;
type A_Handle is access A;
procedure Free is new Ada.Unchecked_Deallocation(A, A_Handle);
Test_Item_Handle : A_Handle;
begin
Test_Item_Handle := new A;
Free(Test_Item_Handle);
end Protected_Final;
내가 얻을이 실행 :
는 "외부"초기화/정리 메시지가 올C:\sandbox\protected_final
Initialize the Map part as instance 1
Initialize the Map part as instance 2
Clean up the Map stuff for instance 2
Clean up the Map stuff for instance 1
[2011-03-04 08:37:29] process terminated successfully (elapsed time: 00.21s)
정적 Test_Item를 선언의 결과
여기에 빠르고 작업 예제 인스턴스 인 반면 내부 쌍은 동적으로 할당되고 할당이 해제 된 Test_Item_Handle에서옵니다.
내가 리눅스에서 valgrind를 통해 이것을 실행할 때 '여전히 도달 할 수있는'유형의 메모리 누수가 발생합니다! "여전히 도달 가능 : 1 블록에서 2,104 바이트"- 그 누수가 어디에서 왔는지 생각할 수 있습니까? – NWS