Re: newbie: deactivate query before freeing?



How about:

myQuery := TQuery.create(nil);
try
myQuery.sql.text := 'select * from blah';
...

finally
freeAndNil(myQuery);
end;

See if I'm right:

This way, if the Create failed, then myQuery is an invalid reference,
and trying to use it will result in an exception, in which case, it
will be assigned nil, but the exception will still be thrown. If the
Create succeeded then the query will execute and so forth. If an
exception occurs in the "try" code, the reference will be freed, and
then the exception will be thrown. If no exceptions are generated, the
reference still gets freed, and everything's fine.

In addition you're saying that the following code is ok:

myQuery := nil;
freeAndNil(myQuery);

But the following code isn't ok?

myQuery := nil;
myQuery.free;

Do I have it right?

-Corinna

.



Relevant Pages

  • Re: Assigned and FreeAndNil
    ... The SysUtils code will set the reference to Nil even if Free raises an ... exception - without the expense of a try/finally block. ...
    (borland.public.delphi.non-technical)
  • Re: Shorter Code Please
    ... In this case the line raises an exception. ... > if Assignedand (c is TLabel) then ... reached the conclusion that having the code raise an exception on the nil ... reference makes more sense. ...
    (alt.comp.lang.borland-delphi)
  • Re: Dataset retunring Null ..a gurus opinion required!
    ... How is estateOleAdapter initialized? ... toolbox to the design area to establish the connection. ... application this exception comes up): ... When adding a reference, I tried "Add Service Reference" and I also ...
    (microsoft.public.dotnet.framework.aspnet.webservices)
  • Re: "invalid handle", sockets, threads and garbage collector
    ... your IntPtr value happens to coincide with a reference. ... The exception contains "invalid handle" message. ... Socket constructor problem ... finalization means closing the handle to resource. ...
    (microsoft.public.dotnet.framework.clr)
  • Global Assembly Cache Issue
    ... I have a dll that I use in several projects, that I placed in the Global ... you can see in the Exception below). ... If I change the Copy Local property of the reference to True, ... Attempting download of new URL ...
    (microsoft.public.dotnet.framework.aspnet)