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

--- Comment #2 from Tobias Burnus <burnus at gcc dot gnu.org> 2012-01-13 
22:17:53 UTC ---
(In reply to comment #1)
> Fixed on trunk as long as explicit allocations are inserted, as below.
> I will raise a separate PR for the lack of automatic allocate on assign for
> class objects with derived type components.

The test case now works since the commit of PR 48351 (and thus I closed PR
51733). The following allocates of comment 1 are still needed, though -
otherwise the code is invalid:
 function multiply(lhs,rhs)
   allocate(multiply)
 program main
   allocate (g%f(2), source = [1.0, 2.0])

 * * *

Unfortunately, the original program still fails. New test case; produces with
ifort 12.1:
   6.000000       12.00000       18.00000    
   24.00000       30.00000       36.00000    

Fails with gfortran at:
  Invalid free() / delete / delete[] / realloc()
    at 0x4C28C3E: free (in
/usr/lib64/valgrind/vgpreload_memcheck-amd64-linux.so)
    by 0x401BD0: MAIN__ (test.f90:34)

The "free" issue only occurs for:
  fireworks = fireworks + fireworks * dt
It works with
  fireworks = fireworks + fireworks
or with
  fireworks = fireworks * dt


I am not sure why it fails, but the following looks a bit odd:

        struct soop_stars D.1952;
        class.8._data = (struct soop_stars *) &D.1952;
        ...
        if (class.8.position.data != 0B)
          __builtin_free ((void *) class.8.position.data);

Shouldn't that be: "class.8._data.position.data" (not the "_data")?


module soop_stars_class
  implicit none
  type soop_stars
    real, dimension(:), allocatable :: position,velocity
  contains
    procedure :: total
    procedure :: product
    generic :: operator(+) => total
    generic :: operator(*) => product
  end type
contains
  type(soop_stars) function product(lhs,rhs)
    class(soop_stars) ,intent(in) :: lhs
    real ,intent(in) :: rhs
    product%position = lhs%position*rhs
    product%velocity = lhs%velocity*rhs
  end function

  type(soop_stars) function total(lhs,rhs)
    class(soop_stars) ,intent(in) :: lhs,rhs
    total%position = lhs%position + rhs%position
    total%velocity = lhs%velocity + rhs%velocity
  end function
end module

program main
  use soop_stars_class ,only : soop_stars
  implicit none
  type(soop_stars) :: fireworks
  real :: dt
  fireworks%position = [1,2,3]
  fireworks%velocity = [4,5,6]
  dt = 5
  fireworks = fireworks + fireworks*dt
  print *, fireworks%position
  print *, fireworks%velocity
end program

Reply via email to