This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Bug fortran/46487] allocatable scalars leak memory (allocatable_scalar_5.f90)


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=46487

--- Comment #1 from Tobias Burnus <burnus at gcc dot gnu.org> 2010-11-16 13:49:58 UTC ---
Well, the problem is actually simple:

program test
  implicit none
  integer :: b
  b = func()
contains
  function func ()
    integer, allocatable ::  func
    allocate(func)
    func = 5332
  end function func
end program test

The function "func" returns the allocatable scalar as:
  return __result_func;

In the main program, one has:
  b = *func ();
or - if "b" is allocatable:
  *b = *func ()

However, the proper method should be:
  {
     tmp = func()
     b = *tmp
     free (tmp);
  }

(Or replace: "free (tmp)" by "if (tmp != NULL) free(tmp)", which is identical
as free also accepts NULL.)

For arrays, it currently works without this detour as there the argument is
passed by value; doing likewise for allocatable scalars would be one -- ABI
breaking -- alternative.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]