<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom">
<title>Comments for Exceptional exceptions</title>
<link rel="alternate" type="text/plain" href="http://edn.embarcadero.com/article/27185" title="Exceptional exceptions" />
<link rel="self" type="application/atom+xml" href="http://edn.embarcadero.com/article/27185/feed" title="Comments for Exceptional exceptions" />
<id>http://edn.embarcadero.com/article/27185</id>
<updated>2009-07-09T13:43:00-07:00</updated>
<entry>
<title>re: Is finally really useful?</title>
<author>
<name>Johannes Berg</name>
<uri>http://threads.embarcadero.com/threads/threads.exe/userall?commentid=31535</uri>
</author>
<id>http://threads.embarcadero.com/threads/threads.exe/view?commentid=31535</id>
<updated>2002-02-20T09:01:48-08:00</updated>
<published>2002-02-20T09:01:48-08:00</published>
<summary>re: Is finally really useful?</summary>
<content>Sure, but one masks the exception and the other lets it continue for further consideration.</content>
</entry>
<entry>
<title>re: Exceptional exceptions - Correction about FreeAndNil Behaviour</title>
<author>
<name>Sean Dockery</name>
<uri>http://threads.embarcadero.com/threads/threads.exe/userall?commentid=31428</uri>
</author>
<id>http://threads.embarcadero.com/threads/threads.exe/view?commentid=31428</id>
<updated>2002-02-10T01:07:29-08:00</updated>
<published>2002-02-10T01:07:29-08:00</published>
<summary>re: Exceptional exceptions - Correction about FreeAndNil Behaviour</summary>
<content>No, the problem was not related to reference counting.The problem was related to the Owner of a Subject in an observer pattern implementation calling FreeAndNil on the Subject field when the Owner was being destroyed.  Observers were notified that the Subject was being destroyed and would try to &quot;unsubscribe&quot; to the Subject by going through Owner.Subject.Unsubscribe.  Owner's Subject reference was already nil at that point because FreeAndNil had nil'd the Subject reference before calling Subject.Free, in which Observers were notified.We consciously planned that Observers would access Subject using Owner.Subject to guard against cases when Subject was nil and because Subjects didn't know anything about Owners, but Observers needed to know about Owners.  Ironically, that conscious plan with good intentions ended up causing problems because Borland named the routine FreeAndNil (instead of NilAndFree.)I don't like FreeAndNil because it makes the decision for me about whether or not it is safe to access an object in an indeterminate state (ie: an object being destroyed) through a reference variable.  I wouldn't care if it wasn't for the routine being poorly named.How many other programmers and going to actively but unknowingly introduce subtle and hard to find bugs into their source code because they will expect a routine named &quot;FreeAndNil&quot; to &quot;Free and then Nil&quot; instead of &quot;Nil and then Free&quot;?  Not because they won't see which line of code is crashing, but because they will resist considering that FreeAndNil is nil'ing and then free'ing because of its name.</content>
</entry>
<entry>
<title>re: Is finally really useful?</title>
<author>
<name>Craven Weasel</name>
<uri>http://threads.embarcadero.com/threads/threads.exe/userall?commentid=31407</uri>
</author>
<id>http://threads.embarcadero.com/threads/threads.exe/view?commentid=31407</id>
<updated>2002-02-07T12:26:31-08:00</updated>
<published>2002-02-07T12:26:31-08:00</published>
<summary>re: Is finally really useful?</summary>
<content>Yes both blocks will execute blahblah.</content>
</entry>
<entry>
<title>Is finally really useful?</title>
<author>
<name>Craven Weasel</name>
<uri>http://threads.embarcadero.com/threads/threads.exe/userall?commentid=31361</uri>
</author>
<id>http://threads.embarcadero.com/threads/threads.exe/view?commentid=31361</id>
<updated>2002-02-04T07:52:51-08:00</updated>
<published>2002-02-04T07:52:51-08:00</published>
<summary>Is finally really useful?</summary>
<content>Is it just me, or are these two blocks of code the same:// ---------------------------------try  blah;finally  blahblah;end;// ---------------------------------try  blah;exceptend;blahblah;</content>
</entry>
<entry>
<title>re: Exceptional exceptions - Correction about FreeAndNil Behaviour</title>
<author>
<name>James Higgins</name>
<uri>http://threads.embarcadero.com/threads/threads.exe/userall?commentid=31349</uri>
</author>
<id>http://threads.embarcadero.com/threads/threads.exe/view?commentid=31349</id>
<updated>2002-02-03T10:51:08-08:00</updated>
<published>2002-02-03T10:51:08-08:00</published>
<summary>re: Exceptional exceptions - Correction about FreeAndNil Behaviour</summary>
<content>Yeah, I saw a problem with that too.  In particular, the example where FreeAndNil is used at the end.  If the Free call raises an exception then the nil part of the suggested FreeAndNil method would never get executed.  Thus the finally block would attempt to free it again.  Thus, if nothing else it should look like this:  try    Strings1.Free;  finally    Strings1 := nil;    end;Sean Dockery, was that bug you fixed by chance related to reference counting?  I've seen similiar strange problems when reference counting is employed.  Then again, if it was reference counted you shouldn't have been freeing it.  Hmm...James Higgins</content>
</entry>
<entry>
<title>Exceptional exceptions - Correction about FreeAndNil Behaviour</title>
<author>
<name>Sean Dockery</name>
<uri>http://threads.embarcadero.com/threads/threads.exe/userall?commentid=30942</uri>
</author>
<id>http://threads.embarcadero.com/threads/threads.exe/view?commentid=30942</id>
<updated>2001-12-23T23:00:04-08:00</updated>
<published>2001-12-23T23:00:04-08:00</published>
<summary>Exceptional exceptions - Correction about FreeAndNil Behaviour</summary>
<content>Pretty solid article, Marcelo.  Kudos...One thing that I did catch: You suggest that FreeAndNil could be emulated by doing the following...Strings1.Free;Strings1 := nil;In reality, FreeAndNil behaves more like this...Hold := Strings1;Strings1 := nil;Hold.Free;The routine should have been called NilAndFree.  :-)I only know this because I fixed bugs in an application that relied on the reference to an object still being valid *while it was being freed*.  The code (which used FreeAndNil) inadvertently caused resource leaks.Ciao.</content>
</entry>
<entry>
<title>Exceptional exceptions - correct sample</title>
<author>
<name>Marcelo Lopez Ruiz</name>
<uri>http://threads.embarcadero.com/threads/threads.exe/userall?commentid=30874</uri>
</author>
<id>http://threads.embarcadero.com/threads/threads.exe/view?commentid=30874</id>
<updated>2001-12-13T11:42:14-08:00</updated>
<published>2001-12-13T11:42:14-08:00</published>
<summary>Exceptional exceptions - correct sample</summary>
<content>I'm working on correcting the HTML tag that is disrupting the code sample. Until then, I'm posting the correct sample here.procedure TForm1.Button5Click(Sender: TObject);var  Strings1: TStringList;  Strings2: TStringList;  Strings3: TStringList;begin  Strings1 := TStringList.Create;  try    Strings2 := TStringList.Create;    try      Strings3 := TStringList.Create;      try        Strings1.Add('Hello, world!');        Strings1.SaveToFile(ChangeFileExt(ParamStr(0), '.txt'));        { This will raise an exception, but all resources will be freed correctly. }        Strings2.Delete(51);      finally        Strings3.Free;      end;    finally      Strings2.Free;    end;  finally    Strings1.Free;  end;end;procedure TForm1.Button4Click(Sender: TObject);var  Strings1: TStringList;  Strings2: TStringList;  Strings3: TStringList;begin  Strings2 := nil;  Strings3 := nil;  Strings1 := TStringList.Create;  try    Strings2 := TStringList.Create;    Strings3 := TStringList.Create;    Strings1.Add('Hello, world!');    Strings1.SaveToFile(ChangeFileExt(ParamStr(0), '.txt'));    { This will raise an exception, but all resources will be freed correctly. }    Strings2.Delete(51);  finally    Strings1.Free;    Strings2.Free;    Strings3.Free;  end;end;</content>
</entry>
<entry>
<title>Exceptional exceptions</title>
<author>
<name>Don Moe</name>
<uri>http://threads.embarcadero.com/threads/threads.exe/userall?commentid=30873</uri>
</author>
<id>http://threads.embarcadero.com/threads/threads.exe/view?commentid=30873</id>
<updated>2001-12-13T06:20:48-08:00</updated>
<published>2001-12-13T06:20:48-08:00</published>
<summary>Exceptional exceptions</summary>
<content>There is a missing &quot;&lt;&quot; at the start of the &quot;pre&quot; directive for the source code in the section &quot;To Protect The Many&quot;. </content>
</entry>
</feed>
