P
Peter Duniho
Ben said:I know exactly what you're getting at, but you and Peter are both wrong.
It must be satisfying to know that, even in a disagreement of semantics
(which is itself almost always a matter of subjective interpretation),
you are always right, and the other person is always wrong.
A memory leak is memory that remains unavailable for reuse after it is no
longer needed. This is the only definition of memory leak that makes sense.
But you haven't defined "remains unavailable".
This is a semantic issue, and we can follow the chain of terminology as
deep as you like. You can't go around saying that your interpretation
is patently obvious while mine is obviously wrong. When dealing with
human language, hardly anything is ever truly obvious.
After all, by your definition, this isn't a memory leak (C++):
void f(void)
{
int* p = new int[1024];
}
Of course that is a memory leak.
That most certainly is a leak by my definition. If you think otherwise,
I obviously haven't explained my definition very well. But that's an
error of communication, not of the definition itself.
And so is this (C#):
class C
{
private static int[] a;
static C() { a = new int[1024]; }
}
C c = new C();
I disagree that that's a leak, sort of.
You still have a variable referencing the memory. But your class is
degenerate, providing no code whatsoever that uses the variable and so
literally speaking I suppose I'd say that's a leak. And it is so by my
definition, since the variable is not accessible by any code in the program.
But assuming you put the private static variable "a" there for a reason,
and assuming you actually wrote code somewhere in the class that uses
"a", then the failure to release the array later once you no longer need
it isn't what I'd call a leak. It's certainly a programmer error, and
it certainly does result in the application using more memory than it
should. But the memory is still accessible by an actual variable
storing the reference to the memory.
These cases are *identical*. Both int arrays are still accessible (in
native code, via HeapWalk, in managed, via reflection on the Type object
which, once loaded, is never freed until the AppDomain unloads), but are
also totally useless in the context given.
I wouldn't call the allocated memory "accessible" in your f() case.
Using HeapWalk or reflection doesn't count as "accessible" to the
program that actually allocated the memory. Or put another way, if you
choose to define memory that can be found via those means as
"accessible", IMHO you've just made the word "accessible" a completely
pointless word, as with a definition that broad (assuming you take it to
its logical conclusion) there is no such thing as memory that is NOT
"accessible".
Pete