Carl Jacobs wrote:
> Well who would have believed that FreeAndNil has a bug in it!

It comes up on the newsgroups often enough.

http://groups.google.com/groups?q=NilAndFree&as_group=*delphi*

But no one has ever reported it to QC. Must not be a big deal.

> The following code shows the essence of the problem.

Looks more like a problem with using global variables, and with assuming 
that the implementation of a function works in a particular way.

You're the one who assumed FreeAndNil would free the object before 
setting the variable to nil. The documentation never made that 
guarantee. It only says the variable will be nil after the function 
terminates. It keeps that promise.

FreeAndNil is working as designed. If there is a bug anywhere, it is in 
the documentation, which fails to make it perfectly clear that once you 
call FreeAndNil, you are not to use the variable anymore.

> If you check the VCL source code (in Delphi 7 at least) then it really does 
> Nil before it Frees.

That's to ensure that the function does what it promised to do, even 
when the object's destructor raises an exception. The implementation is 
a much more efficient way than this:

try
   TObject(Obj).Free;
except
   TObject(Obj) := nil;
   raise;
end;

But you're free to write your own function to use that implementation 
instead.

-- 
Rob


------------------------ Yahoo! Groups Sponsor --------------------~--> 
1.2 million kids a year are victims of human trafficking. Stop slavery.
http://us.click.yahoo.com/.QUssC/izNLAA/TtwFAA/i7folB/TM
--------------------------------------------------------------------~-> 

-----------------------------------------------------
Home page: http://groups.yahoo.com/group/delphi-en/
To unsubscribe: [EMAIL PROTECTED] 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://groups.yahoo.com/group/delphi-en/

<*> To unsubscribe from this group, send an email to:
    [EMAIL PROTECTED]

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.com/info/terms/
 


Reply via email to