sundials icon indicating copy to clipboard operation
sundials copied to clipboard

FCVODE tries to deallocate linear solver and matrix objects twice, leading to errors

Open Nicholaswogan opened this issue 4 years ago • 0 comments

Here is an example program. It tries to to run two integrations with FCVODE.

module vars
  integer*8 neqs
end module

program test
  use vars
  implicit none
  neqs = 1
  call cvoderun(neqs)
  neqs = 2
  call cvoderun(neqs)
end program

subroutine cvoderun(neq)
  implicit none
  integer*8, intent(in) :: neq
  INTEGER*4 IER, METH, IATOL, ITASK
  INTEGER*8 IOUT(25), IPAR(2)
  DOUBLE PRECISION RTOL, ATOL, T0, T, TOUT
  DOUBLE PRECISION U(neq), ROUT(10), RPAR(5)

  t0 = 0
  meth = 1
  iatol = 1
  rtol = 1.d-3
  atol = 1.d-10
  itask = 1
  Tout = 1.d0

  U = 1.d0 ! initial conditions
  CALL FNVINITS(1, NEQ, IER)
  call FSUNDENSEMATINIT(1,neq,neq,ier)
  call FSUNDENSELINSOLINIT(1,ier)
  CALL FCVMALLOC(T0, U, METH, IATOL, RTOL, ATOL, &
                IOUT, ROUT, IPAR, RPAR, IER)
  CALL FCVLSINIT(IER)
  call FCVODE(TOUT, T, U, ITASK, IER)
  print*,U
  call fcvfree
end subroutine


subroutine FCVFUN(T, U, UDOT, IPAR, RPAR, IER)
  use vars
  implicit none
  DOUBLE PRECISION T, U(*), UDOT(*), RPAR(*)
! The following declaration specification should match C type long int.
  INTEGER*8 IPAR(*)
  INTEGER*4 IER
  integer i
  do i=1,neqs
    UDOT(i) = U(i)
  enddo
  ier = 0
end subroutine

However it errors out with

test.run(65387,0x10a85edc0) malloc: *** error for object 0x7fa37b4070b0: pointer being freed was not allocated

The reason is because linear solver and matrix objects are being deallocated two times. The first deallocate occurs in call fcvfree. The second deallocate is occuring, for example, at this line. I'm not sure why the if isn't working properly.

When I comment-out the second deallocations (this line and this line) everything works properly, and valgrind confirms that there are no memory leaks.

Cheers, Nick

Nicholaswogan avatar Apr 13 '21 19:04 Nicholaswogan