Skip to content

Instantly share code, notes, and snippets.

@ytomino
Created December 8, 2012 10:40
Show Gist options
  • Save ytomino/4239744 to your computer and use it in GitHub Desktop.
Save ytomino/4239744 to your computer and use it in GitHub Desktop.
built-in-place bug of gcc-4.7
function alloc return access lifetime.T is
begin
return new lifetime.T'(lifetime.Create);
-- lifetime.Finalize may be called here
end alloc;
with lifetime;
function alloc return access lifetime.T;
with Ada.Text_IO;
with Ada.Unchecked_Deallocation;
with lifetime;
with alloc;
procedure bip_and_finalize is
begin
Ada.Text_IO.Put_Line ("before");
declare
type A is access all lifetime.T;
procedure Free is new Ada.Unchecked_Deallocation (lifetime.T, A);
P : A := A(alloc);
begin
-- P.all may be already finalized!!!
Ada.Text_IO.Put_Line ("lifetime");
Free (P); -- lifetime.Finalize may be called, once again
end;
Ada.Text_IO.Put_Line ("after");
end bip_and_finalize;
-- % gnatmake -gnat2012 bip_and_finalize.adb && ./bip_and_finalize
-- before
-- Initialize (0000000100100090)
-- Finalize (0000000100100090) <- !!!!
-- lifetime
-- Finalize (0000000100100090)
-- after
with Ada.Text_IO;
with System.Address_Image;
package body lifetime is
procedure Finalize (Object : in out T) is
begin
Ada.Text_IO.Put_Line ("Finalize (" & System.Address_Image (Object'Address) & ")");
end Finalize;
function Create return T is
begin
return Result : T := (Ada.Finalization.Limited_Controlled with null record) do
Ada.Text_IO.Put_Line ("Initialize (" & System.Address_Image (Result'Address) & ")");
end return;
end Create;
end lifetime;
with Ada.Finalization;
package lifetime is
type T is limited private;
function Create return T;
private
type T is limited new Ada.Finalization.Limited_Controlled with null record;
overriding procedure Finalize (Object : in out T);
end lifetime;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment