Re: newbie: deactivate query before freeing?
- From: "Maarten Wiltink" <maarten@xxxxxxxxxxxxxxxxxx>
- Date: Thu, 29 Sep 2005 17:00:32 +0200
"swansnow" <schultz@xxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:1128001685.099648.10820@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
[...]
> To me, saying myQuery.free, means "go to the object referred to by
> myQuery and call the free method on it". Well if myQuery is nil, then
> it doesn't refer to anything, so you can't dereference it. I see now
> that free is special, so it's ok, you don't have to explain anymore :)
If you'll bear with me for a few more sentences...
Free isn't actually that special. It checks that 'Self' ("this", to C++
types) isn't nil before proceeding. Any method is free to do that. All
my primary property accessor methods do it. Evaluating a property on a
null object returns a default value, setting the value is simply ignored.
The latter is exactly what Free does.
Evaluating a property, or calling a method, on a null object, is in fact
permitted - sometimes. If you're careful, mostly. The reason is that
you don't "go to the object" in all cases. For virtual methods, you do.
For static methods however, the object's declared type determines which
method will be called. Even if the actual object is a derived type, and
the derived type redefines the method, the original method will be
called. Remember that the method was static; it cannot be overridden.
So for static methods, the call is resolved at compile time. The object
reference doesn't come into it and may even be nil. It is only used as
the first, implicit parameter to the call.
<snip quote from the help file>
> This seems to tell me a couple of things:
> 1) You have to free before you nil, in order to release the memory.
> Calling free on a nil pointer will not result in an error, but the
> object's memory won't get freed, since you lost the reference to it
> when you assigned nil.
That would be rather a silly mistake to make - not that any of us are
above silly mistakes, of course. But your fingers will learn in short
order that ".Free" comes before ":=nil".
BTW, if freeing a variable is the last thing you do with it, you don't
need to clear it. The compiler will even warn you that the assignment
is moot. With a little discipline, most clearing of variables can be
dispensed with.
[...]
> 3) If you Free a form, the memory won't actually get released (because
> the components won't get freed?), so you should say Release, and then
> nil.
Components introduce the "Owner" scheme, where the owning component
will free any components it owns when it's freed itself. Forms are
components and freeing a form will free any components you've designed
onto it.
Release has a different function. You need it when you want to free a
form from one of its event handlers. Since event handlers may be called
in response to Windows messages, and more messages may be queued, more
event handlers may be called after this one. Release defers the actual
freeing until all pending messages have been processed - it does this,
of course, by posting a message to the end of the queue itself, to
which the form responds by falling on its sword.
The truth is that _freeing_ a form from one of its event handlers is
almost universally a bad idea. You might as well close it, and figure
out what to do in the OnClose handler. If you set Action to caFree,
the form will find its own way. One of those ways does in fact call
Release, but that's an internal matter. Release never needed to be
public IMO.
> Maybe I have it now? :)
Umm.... Don't hold your breath. (-:
(That's a *good* thing. Never stop learning.)
Groetjes,
Maarten Wiltink
.
- Follow-Ups:
- Re: newbie: deactivate query before freeing?
- From: swansnow
- Re: newbie: deactivate query before freeing?
- References:
- newbie: deactivate query before freeing?
- From: swansnow
- Re: newbie: deactivate query before freeing?
- From: Maarten Wiltink
- Re: newbie: deactivate query before freeing?
- From: swansnow
- Re: newbie: deactivate query before freeing?
- From: Maarten Wiltink
- Re: newbie: deactivate query before freeing?
- From: swansnow
- newbie: deactivate query before freeing?
- Prev by Date: Re: newbie: deactivate query before freeing?
- Next by Date: List of shapes
- Previous by thread: Re: newbie: deactivate query before freeing?
- Next by thread: Re: newbie: deactivate query before freeing?
- Index(es):
Relevant Pages
|
|