Re: pointer components and memory leaks



On Sep 17, 12:36 am, "mreste...@xxxxxxxxx" <mreste...@xxxxxxxxx>
wrote:
On 16 Sep., 21:13, nos...@xxxxxxxxxxxxx (Richard Maine) wrote:



Paul van Delst <Paul.vanDe...@xxxxxxxx> wrote:

Richard Maine wrote:
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?
...
and I still get the memory leak in tt_sum. It seems to me that this
example will always leak somewhere inbetween the "+" and the "=".
(only tried one compiler)
 ...
The line
   c=a+b
first calls tt_sum to do the "a+b" bit. Then tt_def is called to do the
"c=..." bit. But what(or where) is the rhs of the assignment? The z result
of tt_sum becomes the x input of tt_def...but where does the x input of
tt_def "go" after the assignment is done?

Crikey, trying to explain what I think I mean is confusing me.

That's ok. I'm following, at least adequately. I had forgotten all the
details, but I think you are right in that you also need a final
procedure to make that work without leaks. I haven't checked the exact
rules, but that's bound to be a context in which a final procedure is
invoked; anyway, it better be, that being one of the points of them. The
result of a+b is a temporary, which "goes away" after the assignment.
Final procedures are supposed to be called whenever something "goes
away" (though, of course, that's not the formal terminologuy used).

Richard, Paul,
   thank you for the illuminating comments. My question originated
while working on a rather large code which predates the TR 15581,
where many pointers components "would like" to be allocatables... but
they are not. I was focusing on the issue of the temporary, but indeed
after reading Richard Maine's post I see many other potential issues,
which are addressed in Paul van Delst's version of the code. All in
all, I think that for the time being I will use a somewhat more
conservative approach with dedicated procedures to make sums and
assignment, avoiding the overloaded operators, and thus temporary
objects (being careful about initializations and deallocations!).

Marco

Also, FYI, I have found the g95 compiler extremely helpful in tracking
down memory leaks. At the end of a run, it lists out the lines where
allocations occurred that were never deallocated.

On a tangentially related point, if you have occasion to write longer
expressions with defined operators, you'll eventually encounter the
need to dynamically discover the resulting call tree because it will
change every time you re-order terms. I recommend adding a globally
accessible debugging flag in your code:

module global_parameters
implicit none
logical, parameter :: debugging=.true.
end module

Then I would use this module in every other module and add conditional
print statements at the beginning and end of each procedure:

subroutine foo()
if (debugging) print *,'foo start'
...
if (debugging) print *,'foo finish'
end subroutine

When debugging=.false., most compilers will eliminate such statements
completely during their "dead code removal" optimization pass since
"debugging" is a compile-time constant. Thus, there is no run-time
penalty on production code.

Damian
.



Relevant Pages

  • Re: pointer components and memory leaks
    ... But whatis the rhs of the assignment? ... procedure to make that work without leaks. ... I have made this approach work with every compiler I've ... G.W. Stewart, Memory leaks in derived types revisited, ACM Fortran ...
    (comp.lang.fortran)
  • Re: How to implement dynamic typing?
    ... but more efficiently than that looks (ptemp stays in a register). ... assignment opcodes. ... like my early C compiler, registers were assigned to particular fixed uses, ... different strategy, namely having built-in "core" machinery, and leaving the ...
    (comp.compilers)
  • Re: Requesting advice how to clean up C code for validating string represents integer
    ... You consider a compiler to be stupid for following the language ... How can an array be an lvalue, if you can't assign to it? ... side of an assignment. ...
    (comp.lang.c)
  • Re: Python and Flaming Thunder
    ... the compiler optimizes it away, ... I think overloading your catch error types to include objects ... assignment could throw an error) makes things confusing. ...
    (comp.lang.python)
  • Re: PL/I DATA TYPES & DEFAULTS
    ... In performing assignments, if the value to be assigned is not of the same type as the target of the assignment, PL/I will attempt to convert the value to the required type. ... The storage representation of such a variable in the workstation implementations of PL/I is 7 nibbles to represent the 7 digits in big endian order, followed by 1 nibble to represent the algebraic sign, making four bytes all together. ... If the programmer writes the statements ... The compiler doesn't guess types. ...
    (comp.lang.pl1)