创建受控类型将在返回

时间:2017-10-19 14:23:12

标签: factory ada dangling-pointer finalization

我想以下列方式创建一个用于创建和初始化受控类型(有点像工厂)的函数:

function Create return Controlled_Type
is
  Foo : Controlled_Type;
begin
   Put_Line ("Check 1")
   return Foo;
end Create;

procedure Main
is
  Bar : Controlled_Type := Create;
begin
  Put_Line ("Check 2")
end Main;

output:
Initialize
Check 1
Adjust
Finalize

由于finalize将处理一些在受控类型中指向的对象,我最终会在Bar中悬挂指针,并且不知何故这会立即崩溃程序,因此我从未看到“Check 2”。

使用新的Controlled_Type并在Create函数中返回指针可以很容易地解决这个问题。但是,我喜欢使用受控类型而不是指向它的指针,因为当Bar超出范围时,将自动调用终结。如果Bar是指针,我必须手动处理它。

有没有办法正确地做到这一点而没有悬挂指针?我应该在调整程序中做些什么吗?

1 个答案:

答案 0 :(得分:3)

好吧,你应该适当地实施Adjust

复制时,它是按位的,因此原件中的任何指针都按原样复制到副本中。当原始文件被完成并且指向的对象被释放时,你会在副本中留下一个指向超空间的指针。

要做的是分配一个新指针,指定与原始值相同的值。像

这样的东西
with Ada.Finalization;
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Unchecked_Deallocation;

procedure Finalart is

   type Integer_P is access Integer;
   type Controlled_Type is new Ada.Finalization.Controlled with record
      P : Integer_P;
   end record;
   procedure Initialize (This : in out Controlled_Type);
   procedure Adjust (This : in out Controlled_Type);
   procedure Finalize (This : in out Controlled_Type);

   procedure Initialize (This : in out Controlled_Type) is
   begin
      Put_Line ("initialize");
      This.P := new Integer'(42);
   end Initialize;

   procedure Adjust (This : in out Controlled_Type) is
      Original_Value : constant Integer := This.P.all;
   begin
      Put_Line ("adjust");
      This.P := new Integer'(Original_Value);
   end Adjust;

   procedure Finalize (This : in out Controlled_Type) is
      procedure Free is new Ada.Unchecked_Deallocation (Integer, Integer_P);
   begin
      Put_Line ("finalize");
      Free (This.P);
   end Finalize;

   function Create return Controlled_Type is
      CT : Controlled_Type;
   begin
      Put_Line ("check 1");
      return CT;
   end Create;

   Bar : Controlled_Type := Create;
begin
   Put_Line ("check 2");
end Finalart;

如果我在This.P := new Integer'(Original_Value);注释掉Adjust行,我会(在macOS上)

$ ./finalart 
initialize
check 1
adjust
finalize
adjust
finalize
finalart(35828,0x7fffd0f8b3c0) malloc: *** error for object 0x7fca61500000: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug

raised PROGRAM_ERROR : unhandled signal