https://gcc.gnu.org/bugzilla/show_bug.cgi?id=61831
--- Comment #45 from Dominique d'Humieres <dominiq at lps dot ens.fr> --- > Created attachment 34942 [details] > Better patch Sorry for the delay, but I noticed this new patch only yesterday!-( > I'm not working on this, so I'm attaching the current patch in my work tree, > before it's lost. With this patch there is no memory leak gfortran.dg/alloc_comp_constructor_1.f90 (19 builtin_free) and gfortran.dg/class_array_15.f03 (12 builtin_free). > If I remember correctly, the patch passes the testsuite without regressing. With this patch I see a regression for gfortran.dg/alloc_comp_assign_10.f90. > The test in comment #41 exhibits some leaks with it though. With the patch I see 33 builtin_free instead of 34 without the patch. > And I haven't looked yet at Dominique's feedback in comment #43. The test in comment #41 fails at run time when compiled with -fsanitize=address. If I take the "complement" of the reduced test posted in comment #43, everything works fine, but for one builtin_free less. I did not investigated what is wrong with the test in comment #43 (will do). > Oh, and I have to double check that the gfc_trans_subarray_assign hunk > is really necessary. If you are speaking of the hunk @@ -6263,7 +6283,7 @@ gfc_trans_subarray_assign (tree dest, gfc_componen gfc_conv_expr (&rse, expr); - tmp = gfc_trans_scalar_assign (&lse, &rse, cm->ts, true, false, true); + tmp = gfc_trans_scalar_assign (&lse, &rse, cm->ts, true, true, true); gfc_add_expr_to_block (&body, tmp); gcc_assert (rse.ss == gfc_ss_terminator); I think it is needed, otherwise the test in comment #41 fails at run time with a.out(89696,0x7fff74744300) malloc: *** mach_vm_map(size=18446603338973675520) failed (error code=3) *** error: can't allocate region *** set a breakpoint in malloc_error_break to debug Program received signal SIGSEGV: Segmentation fault - invalid memory reference. However the hunk @@ -4990,7 +5010,7 @@ gfc_conv_procedure_call (gfc_se * se, gfc_symbol * tmp = gfc_deallocate_alloc_comp (e->ts.u.derived, tmp, parm_rank); - gfc_add_expr_to_block (&se->post, tmp); + gfc_prepend_expr_to_block (&se->post, tmp); } /* Add argument checking of passing an unallocated/NULL actual to does not seems necessary, but is the cause of the regression, i.e., no regression without it. The patch (without the above hunk) fixes a lot of problems and makes the code simpler. IMO it should be submitted and the few left issues can be deferred (with new PRs).