[Bug fortran/54784] [OOP] allocation of extended types with polymorphic allocatable members

2012-10-02 Thread jkozdon at gmail dot com


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



--- Comment #2 from Jeremy Kozdon  2012-10-02 
22:14:57 UTC ---

So there is actually two different bugs, one is the invalid memory reference

which happens when you don't allocate in order.



There is a second, the one I was really trying to report, and that's when

allocating in my original example type(block1d) before type(block2d), namely

the call

  call d%addBlock(1,b1)

  call d%addBlock(2,b2)

as opposed to

  call d%addBlock(1,b2)

  call d%addBlock(2,b1)



This gives the error:

a.out(4678) malloc: *** error for object 0x7fff82ec09be: pointer being freed

was not allocated

*** set a breakpoint in malloc_error_break to debug



Program received signal SIGABRT: Process abort signal.



Backtrace for this error:

#0  0x14fbe

#1  0x156d4

#2  0x7fff82f1f1b9

Abort trap



(In reply to comment #1)

> Thanks for reporting this. I can reproduce it with 4.7 and trunk. Here is a

> reduced test case:

> 

> program bug

>   implicit none

> 

>   type :: block

> real, allocatable :: fields

>   end type

> 

>   type :: list

> class(block),allocatable :: B

>   end type

> 

>   type :: domain

> type(list),dimension(2) :: L

>   end type

> 

>   type(domain) :: d

>   type(block) :: b1

> 

>   allocate(d%L(2)%B,source=b1)

> 

> end program  bug 

> 

> 

> 

> This fails at runtime with:

> 

> Program received signal SIGSEGV: Segmentation fault - invalid memory 
> reference.

> 

> Backtrace for this error:

> #0  0x7F379D1FDA97

> #1  0x7F379D1FE074

> #2  0x7F379C72ED9F

> #3  0x400804 in __copy_bug_Block at bug.f90:9

> #4  0x40097C in bug at bug.f90:19 (discriminator 2)

> Segmentation fault

> 

> 

> valgrind shows:

> 

> ==11046== Invalid read of size 8

> ==11046==at 0x400804: __copy_bug_Block (bug.f90:9)

> ==11046==by 0x40097C: MAIN__ (bug.f90:19)

> ==11046==by 0x400A88: main (bug.f90:21)

> ==11046==  Address 0x0 is not stack'd, malloc'd or (recently) free'd


[Bug fortran/54784] New: [oop] allocation of extended types with polymorphic allocatable members

2012-10-02 Thread jkozdon at gmail dot com


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



 Bug #: 54784

   Summary: [oop] allocation of extended types with polymorphic

allocatable members

Classification: Unclassified

   Product: gcc

   Version: 4.8.0

Status: UNCONFIRMED

  Severity: normal

  Priority: P3

 Component: fortran

AssignedTo: unassig...@gcc.gnu.org

ReportedBy: jkoz...@gmail.com





(Error also occurs with gcc 4.7.1)



When I have a container module with stores a collection of polymorphics types,

I sometimes get an error that a pointer is being freed which was not allocated. 



An example program is below. It can be compiled without errors

   gfortran bug.f90



It contains four modules each with an associated type. If the type 'block' in

'block_module' the member nFields is commented out the code runs fine.



If a print statement is added in the program the code runs fine



If the dimension of the member 'x' of types block_cart1d and block_cart2d are

changed so that they are not 3 and 4 (only one must be changed), the program

works.



Finally, if the order in the program of the addBlock command is reversed the 



bug.f90



module block_module

  implicit none

  private



  public :: block

  type,abstract :: block

! if commented out code works fine

integer   ,private :: nFields  = 0

  end type block



end module block_module



module block1d_module

  use block_module, only : block



  type,extends(block) :: block1d

real,dimension(:,:,:),allocatable,private :: fields

  end type

end module



module block2d_module

  use block_module, only : block



  type,extends(block) :: block2d

real,dimension(:,:,:,:),allocatable,private :: fields

  end type

end module





module domain_module

  use block_module, only : block



  implicit none

  type :: list

class(block),allocatable :: B

  end type



  type :: domain

type(list),dimension(10) :: L

  contains

procedure :: addBlock

  end type

contains



  subroutine addBlock(this,i,b)

implicit none

class(domain),intent(inout) :: this

integer,intent(in) :: i

class(block),intent(in) :: b



allocate(this%L(i)%B,source=b)

  end subroutine

end module



program bug

  use domain_module, only : domain

  use block1d_module, only : block1d

  use block2d_module, only : block2d

  implicit none

  type(domain) :: d

  type(block1d) :: b1

  type(block2d) :: b2



  ! crashes with "pointer being freed was not allocated"

  call d%addBlock(1,b1)

  call d%addBlock(2,b2)



  ! crashes with "invalid memory reference"

  ! call d%addBlock(2,b2)

  ! call d%addBlock(1,b1)



  ! runs fine

  ! call d%addBlock(1,b2)

  ! call d%addBlock(2,b1)



end program  bug