[Ada] Memory leak mixing limited and nonlimited functions
This patch fixes a memory leak. If a build-in-place function with a result whose size is not known at the call site is called, and that function calls a non-build-in-place function that allocates on the secondary stack, the secondary stack was not necessarily cleaned up, which caused a memory leak. The following program should print: "Current allocated space : 0 bytes" (among other things) in the loop. ./bip_leak-main > log grep 'Current allocated' log Current allocated space : 0 bytes Current allocated space : 0 bytes Current allocated space : 0 bytes with Ada.Finalization; package BIP_Leak is subtype Limited_Controlled is Ada.Finalization.Limited_Controlled; type Nonlim_Controlled is new Ada.Finalization.Controlled with null record; type Needs_Fin is record X : Nonlim_Controlled; end record; type Lim_Controlled is new Limited_Controlled with null record; function Return_Lim_Controlled (Source : Boolean) return Lim_Controlled; procedure Dump_SS; end BIP_Leak; with Ada.Text_IO; pragma Warnings (Off); with System.Secondary_Stack; pragma Warnings (On); package body BIP_Leak is function Transform (X : Needs_Fin) return Lim_Controlled is begin return (Limited_Controlled with null record); end; function Return_Needs_Fin (I : Boolean) return Needs_Fin is THR : Needs_Fin; begin return THR; end; function Return_Lim_Controlled (Source : Boolean) return Lim_Controlled is begin return Transform (Return_Needs_Fin (Source)); end Return_Lim_Controlled; procedure Dump_SS_Instance is new System.Secondary_Stack.SS_Info (Ada.Text_IO.Put_Line); procedure Dump_SS renames Dump_SS_Instance; end BIP_Leak; procedure BIP_Leak.Main is begin for Count in 1 .. 350_000 loop declare Msg : constant Lim_Controlled := Return_Lim_Controlled (True); begin if Count mod 100_000 = 0 then Dump_SS; end if; end; end loop; end BIP_Leak.Main; 2018-05-24 Bob Duff <duff@adacore.com> gcc/ada/ * exp_ch7.adb (Expand_Cleanup_Actions): Create a mark unconditionally for build-in-place functions with a caller-unknown-size result. (Create_Finalizer): For build-in-place functions with a caller-unknown-size result, check at run time whether we need to release the secondary stack. From-SVN: r260650
Showing
Please
register
or
sign in
to comment