https://gcc.gnu.org/bugzilla/show_bug.cgi?id=123483
--- Comment #2 from Andrew Benson <abensonca at gcc dot gnu.org> --- I spent some time tracing through gfortran to try to figure out why this occurs. From what I was able to figure out, the problem arises because gfc_conv_procedure_call is used to generate the call to the defined assignment subroutine (baseAssignment). Because the worker type has an allocatable array component this results in gfc_deallocate_alloc_comp being called to deallocate those deallocatable components. This in turn calls structure_alloc_comps which recursively deallocates all components of the worker type. The problem is that structure_alloc_comps is also finalizing any finalizable components, but it seems that these finalizable components are being finalized elsewhere, so now we get a duplicated finalization. I noticed that structure_alloc_comps has a no_finalization option, which defaults to false. So, I simply tried setting this to true in the call to structure_alloc_comps from within gfc_deallocate_alloc_comp (adding a no_finalization argument that also defaults to false to gfc_deallocate_alloc_comp). This seems to fix the problem, resulting in: assignment - begin workConstructor - begin resourceManagerConstructor 1 resourceManagerAssign 2 resourceManagerDestructor 1 workConstructor - end baseAssignment resourceManagerAssign 2 resourceManagerDestructor 1 assignment - end and valgrind shows no memory leaks. I also checked and found no regressions with this fix. The patch is attached.
