Re: Overloading OPERATOR(+): my usage causing memory leaks.



Paul van Delst <paul.vandelst@xxxxxxxx> wrote:

I'm overloading an elemental ADD function to OPERATOR(+) and I'm getting
Imemory leaks when apply it to array arguments. Scalar arguments cause no
Iproblem.

My question is: what in the code could be causing these memory leaks?

In the test program, if I comment out the
c = c + d
line, i.e. adding the array structures, then I get no memory leaks. I made
all the routines in question elemental, so they should work with array
arguments, right?

[code and valgrind output elided]

I can't get anything from the valgrind output, probably because I've
never used valgrind. I'm not a beer drinker anyway. :-)

I'd guess it to be a compiler bug, and I can even roughly guess where.
I'm not at all sure, but it seems a good enough guess to mention.

I think it is the compiler failing to remove a compiler temporary. Your
addition of c+d is quite likely to generate a temporary array for that
sum. Then that temporary array will be assigned to c (using your defined
assignment). The compiler *OUGHT* to deallocate the temporary after the
assignment, but it seems a good candidate for something some compilers
might miss.

P.S. As far as I can see, your defined assignment does the same thing as
the intrinsic assignment would... except that the compiler might have a
better chance of being smart about optimizing the intrinsic one, whereas
is seems likely to just "punt" and naively invoke your defined one. Of
course, if this is just a simplification of code that involves other
things, that would be different. Or perhaps variants of this are also
intended to work with pointer components instead of allocatable ones;
pointer components tend to need more of this kind of stuff to fake what
allocatable ones do intrinsically.

--
Richard Maine | Good judgment comes from experience;
email: last name at domain . net | experience comes from bad judgment.
domain: summertriangle | -- Mark Twain
.



Relevant Pages

  • Re: fdgrowtable() cleanup
    ... It is a dereference before the assignment, so I perceive it as the ... No, I do not think that compiler can move the dereference, ... Sequence points are a language feature and are only meaningful in the ... the struct freetable between the file array and the flag array. ...
    (freebsd-hackers)
  • Re: Overloading OPERATOR(+): my usage causing memory leaks.
    ... Imemory leaks when apply it to array arguments. ... I'd guess it to be a compiler bug, and I can even roughly guess where. ... assignment, but it seems a good candidate for something some compilers ... where a and b are derived types with allocatable components, the compiler should allocate ...
    (comp.lang.fortran)
  • Re: c = inverse(sqrt(epsilon nought *mu nought))
    ... error 381 - POSITION is not an array. ... One result of that is that the compiler can get ... interpret it as an assignment. ... Statement functions must be ...
    (comp.lang.fortran)
  • Re: gfortran problem
    ... descriptor for the lhs array has to be touched to get the array's ... Maybe in many cases the compiler ... for a reallocating assignment, or maybe, in the spirit of pointer ...
    (comp.lang.fortran)
  • Re: Unexplainable Behaviour with Array Assigment on Intel 9 Compiler
    ... My guess is that it is stack overflow from a temporary array ... the implicit array assignment as a matter of style but now I am ... seem to be "handled better" (for the Intel compiler anyway). ... while the explicit do loop doesn't. ...
    (comp.lang.fortran)