Re: newbie: deactivate query before freeing?
- From: "swansnow" <schultz@xxxxxxxxxxxxxxxxxxxxxxx>
- Date: 29 Sep 2005 06:48:05 -0700
> Free is *always* safe. I think I said that,
Yes, you did... I've been quite confused on this issue for several
reasons: my background is C, where you have to be alert to
dereferencing pointers to memory you shouldn't access; the Delphi help
is confusing (quoted below, and then another comment by me)...
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 :)
However, to make sure that the memory gets freed, you would have to
call Free before you assign nil. (Either way, though, you won't get an
exception.)
{QUOTE follows}
procedure FreeAndNil(var Obj);
Description
Use FreeAndNil to ensure that a variable is nil after you free the
object it references. Pass any variable that represents an object as
the Obj parameter.
Warning: Obj must be an instance of a TObject descendent class..
Procedure Free;
Use Free to destroy an object. Free automatically calls the destructor
if the object reference is not nil. Any object instantiated at runtime
that does not have an owner should be destroyed by a call to Free so
that it can be properly disposed of and its memory released. Unlike
Destroy, Free is successful even if the object is nil; so if the object
was never initialized, Free won't result in an error. ...
To free a form, call its Release method, which destroys the form and
releases the memory allocated for it after all its event handlers and
those of the components it contains are through executing.
{End QUOTE}
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.
2) FreeAndNil is a shortcut that does the freeing and nil-ling in to
proper order, in a single statement.
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.
Maybe I have it now? :)
-Corinna
.
- Follow-Ups:
- Re: newbie: deactivate query before freeing?
- From: Maarten Wiltink
- 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
- newbie: deactivate query before freeing?
- Prev by Date: Re: newbie: deactivate query before freeing?
- Next by Date: Re: newbie: deactivate query before freeing?
- Previous by thread: Re: newbie: deactivate query before freeing?
- Next by thread: Re: newbie: deactivate query before freeing?
- Index(es):
Relevant Pages
|