Board index » delphi » Bad References to TForm

Bad References to TForm

This is probably *really* obvious, but here goes...

Is there a straight-forward way to determine if a reference to a TForm is
valid. In other words, if I have code that calls AnyForm.Free, is there a
way that I can make sure AnyForm hasn't already been freed (besides trying
it and catching the exception)? TIA.
--
Trey Campbell
campb...@softdisk.com

 

Re:Bad References to TForm


There is a zip file of components I use everyday on my homepage.  Included
is a unit called Tools.PAS which has two very useful and simple routines
you can use to solve this problem.

--
Mark Pritchard
-------------
http://www.ozemail.com.au/~pritchma

Trey Campbell <campb...@softdisk.com> wrote in article
<01bbae23$c1f3f660$7d6d1cce@tcampbell>...

Quote
> This is probably *really* obvious, but here goes...

> Is there a straight-forward way to determine if a reference to a TForm is
> valid. In other words, if I have code that calls AnyForm.Free, is there a
> way that I can make sure AnyForm hasn't already been freed (besides
trying
> it and catching the exception)? TIA.
> --
> Trey Campbell
> campb...@softdisk.com

Re:Bad References to TForm


Quote
Trey Campbell wrote:
> Is there a straight-forward way to determine if a reference to a TForm is
> valid. In other words, if I have code that calls AnyForm.Free, is there a
> way that I can make sure AnyForm hasn't already been freed (besides trying
> it and catching the exception)? TIA.

Under 3.x, there are a Win API calls named something like
IsValidReadPtr and IsValidWritePtr - alas, my cursory search of the D2 help
didn't turn them up. Of course, neither will help you if the form has been
freed but the memory has since been reallocated.

Otoh, it sounds like what you're really asking is not "this form may or
may not have been freed, can I still refer to it?" but "is it safe to Free
this form, or has that already happened?" In the latter case, it may make
sense to use a construct like "AnyForm.Free; AnyForm := Nil;", so that it
will be safe to call Free twice. Of course, if AnyForm is just a copy of
another pointer to the form, this won't help you.

--

http://www.midnightbeach.com/jon   Personal Pages
http://www.midnightbeach.com/jon/pubs Programming Publications
http://www.midnightbeach.com/hs             Home School Resource List

Other Threads