https://gcc.gnu.org/bugzilla/show_bug.cgi?id=110547
--- Comment #3 from Andrew Benson <abensonca at gcc dot gnu.org> ---
(In reply to Jürgen Reuter from comment #2)
> This would support that gfortran and NAG are correct, no?
Yes, I agree: this is not a gfortran bug, and I'm closing it.
The task in taskerCompute is orphaned and has no DEFAULT clause, and OpenMP
makes a dummy argument passed by reference firstprivate in that case. So each
task gets its own copy of self, and that copy is finalized when the task ends.
That gives exactly the two extra finalizer calls in my test case, and NAG gives
the same output.
Making the sharing explicit gives the output I originally expected (one
finalizer call, from the assignment) with 16.2.1 and 17.0:
!$omp task untied shared(self)
call self%compute()
!$omp end task
That is safe here because every task completes inside the TASKGROUP in the main
program, while tasker_ still exists. In general a shared dummy argument is only
safe if the task completes before the object it refers to goes away (for
example, by adding a TASKWAIT before the procedure returns). Another option is
to give the task a local pointer to self, which can safely be firstprivate:
class(tasker), pointer :: self_
self_ => self
!$omp task untied firstprivate(self_)
call self_%compute()
!$omp end task
(self must then have the TARGET attribute.) This is the approach we have now
taken in our own code.
Thanks for looking into this, and for checking it with NAG.