Re: pointer components and memory leaks
- From: Damian <damian@xxxxxxxxxx>
- Date: Wed, 17 Sep 2008 03:00:36 -0700 (PDT)
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
.
- References:
- pointer components and memory leaks
- From: mrestelli@xxxxxxxxx
- Re: pointer components and memory leaks
- From: Richard Maine
- Re: pointer components and memory leaks
- From: Paul van Delst
- Re: pointer components and memory leaks
- From: Richard Maine
- Re: pointer components and memory leaks
- From: mrestelli@xxxxxxxxx
- pointer components and memory leaks
- Prev by Date: Re: pointer components and memory leaks
- Next by Date: Re: traverse linked list (Fortran 90)
- Previous by thread: Re: pointer components and memory leaks
- Next by thread: Re: pointer components and memory leaks
- Index(es):
Relevant Pages
|