Re: pointer components and memory leaks



On 16 sep, 19:06, "mreste...@xxxxxxxxx" <mreste...@xxxxxxxxx> wrote:
Dear all,
   is there a way to overload the operators +, =, ... for a derived
type with pointer components which does not leak memory?

To be more precise, the attached code, according to valgrind, leaks
the memory allocated in tt_sum (which indeed seems reasonable to me).
Is there a way to rearrange this code to avoid memory leaks?

I have to add that 1) in general I can not switch to allocatable
components 2) I think a FINAL procedure might solve the problem, but I
wonder how many compilers support it.

Thank you for any advice,
   Marco

module mm

 implicit none

 type tt
   real, pointer :: x(:)
   !real, allocatable :: x(:)
 end type tt

 interface operator(+)
   module procedure tt_sum
 end interface

 interface assignment(=)
   module procedure tt_def
 end interface

contains

 function tt_sum(x,y) result(z)
  type(tt), intent(in) :: x,y
  type(tt) z

   allocate(z%x(size(x%x)))
   z%x = x%x + y%x

 end function tt_sum

 subroutine tt_def(y,x)
  type(tt), intent(out) :: y
  type(tt), intent(in) :: x

   allocate(y%x(size(x%x)))
   y%x = x%x
 end subroutine tt_def

end module mm

program test

 use mm

 implicit none

 type(tt) :: a,b,c

 allocate(a%x(5),b%x(5))
 a%x = (/ 1.0 , 2.0 , 3.0 , 4.0 , 5.0 /)
 b%x = -2.0

 c = a+b

 deallocate(a%x,b%x,c%x)

end program test

In addition to the other responses, you might try to use
a memory pool - I posted a program several weeks ago to demonstrate
that in a similar thread.

See: http://groups.google.com/group/comp.lang.fortran/browse_frm/thread/2cb7401441ee0a2d?hl=nl#

Regards,

Arjen
.



Relevant Pages

  • Re: Eels of Atlantis revisited
    ... Eels have been ruled by instinct since the Cretaceous Age. ... "The eel has, it seems, a better memory than man. ...      Memory and instinct are two different things. ... from fresh water rivers to salt water oceans. ...
    (talk.origins)
  • Re: memory leak using system.windows.forms.timer
    ... subroutine, memory leaks. ...     Private Sub CheckComputers() ... subroutine with a static number I still leak memory. ...
    (microsoft.public.dotnet.languages.vb)
  • Re: memory leak using system.windows.forms.timer
    ... subroutine, memory leaks. ... Private Sub CheckComputers() ... Dim intCount As Integer ...     Private MasterTimer As System.Timers.Timer ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Problem with RWMPB
    ...     Whilst running an image, it hangs and shows a status of RWMPB (Modified ... As has been noted, RWMPB means that a page fault has occurred, and the ... or memory to hold the page is not ... initialize the data structures ...
    (comp.os.vms)
  • Re: SROM port pinout for Digital PWS
    ... If I remember rightly, the SROM port expects a loader program, ... console (nb please verify with mini-debugger docs below as this is ... Addr    fffffc87.c007ffff ... Memory type and speed and various other not- ...
    (comp.sys.dec)