 |
BorlandTalk.com Borland discussion newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
George Birbilis Guest
|
Posted: Sat Jan 29, 2005 11:49 pm Post subject: calling FreeAndNil on an interface |
|
|
on D7, it seems that calling FreeAndNil on an interface type crashes the
application with access violation. If run from outside Delphi throws
infinite number of error windows till you kill the app
if anyone can reproduce it on latest Delphi, please fill-in bug report (too
busy at the moment and afraid I'll forget to do it myself later on)
//---------------------------
unit _3impact_EditorManager;
interface
type
T3ImpactObjectEditorManager=class(TInterfacedObject,I3ImpactObjectEditorManager) //...end;var defaultEditorManager:I3ImpactObjectEditorManager;implementation//...initialization defaultEditorManager:=T3ImpactObjectEditorManager.Create;finalization defaultEditorManager:=nil;//never call FreeAndNil on an interface reference,//just set it to "nil" to release the respective objectend.//---------------------------if at the "finalization" section above you do FreeAndNil(defaultEditorManager)doom happens (and call stack window shows nothing - had to put a break pointat one of the "finalization" sections in one of my units and then move stepby step all through all actions Delphi does at the app closing till foundwhere it was crashing the app)-----George Birbilis (birbilis (AT) kagi (DOT) com)http://www.kagi.com/birbilis--------------
|
|
| Back to top |
|
 |
John Carlyle-Clarke Guest
|
Posted: Mon Jan 31, 2005 2:23 pm Post subject: Re: calling FreeAndNil on an interface |
|
|
"George Birbilis" <birbilis (AT) kagi (DOT) com> wrote in
news:41fe2cfd (AT) newsgroups (DOT) borland.com:
| Quote: | on D7, it seems that calling FreeAndNil on an interface type
crashes the application with access violation. If run from outside
Delphi throws infinite number of error windows till you kill the
app
|
From the D5 help:
"procedure FreeAndNil(var Obj);
"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: Do not pass a value for Obj if it is not an instance of
TObject or one of its descendants."
In the source, there is a comment:
{ FreeAndNil frees the given TObject instance and sets the variable
reference to nil. Be careful to only pass TObjects to this routine. }
The warning is there because there is no type checking on the call.
Thinking about it, I can't see why they didn't make it:
procedure FreeAndNil(var Obj : TObject);
...but maybe there is a good reason I haven't considered.
Anyway, the source is very simple:
procedure FreeAndNil(var Obj);
var
P: TObject;
begin
P := TObject(Obj);
TObject(Obj) := nil;
P.Free;
end;
This will definitely not work with an interface!
So, IMO this is not a bug. FreeAndNil is not designed for interfaces,
will not work with them. In fact, the idea of "free and nil" is not
really compatible with a reference counted interface reference.
But yes, tracking down bugs in finalization code is really hard :)
|
|
| Back to top |
|
 |
George Birbilis Guest
|
Posted: Mon Jan 31, 2005 5:44 pm Post subject: Re: calling FreeAndNil on an interface |
|
|
| Quote: | So, IMO this is not a bug. FreeAndNil is not designed for interfaces,
will not work with them. In fact, the idea of "free and nil" is not
really compatible with a reference counted interface reference.
|
crashing down the Delphi app and poping up infinite number of illegal access
error dialogs is a bug though - extremely bad crashing behaviour
the problem occured cause I had changed an object reference to be an
interface type (since that object did support an interface), to make the
code style cleaner and more flexible, but had forgotten to remove that
"FreeAndNil(something)" and just use "something:=nil" at the "finalization"
section of that unit
I don't see why they do
procedure FreeAndNil(var Obj);
var
P: TObject;
begin
P := TObject(Obj);
TObject(Obj) := nil;
P.Free;
end;
and not
procedure FreeAndNil(var Obj:PObject);
var
P:PObject;
begin
P := Obj;
Obj:=nil;
P^.Free;
end;
so that they have compile-time safety for that procedure
| Quote: | But yes, tracking down bugs in finalization code is really hard
|
:-}
maybe they should be releasing the default exception handling stuff always
last so that a decent error message is shown or at least a decent closing of
the process happens. Or is it similar to how Java docs say, not designed for
airline traffic control, nuclear facilities operation etc. ;o)
-----
George Birbilis (birbilis (AT) kagi (DOT) com)
http://www.kagi.com/birbilis
--------------
|
|
| Back to top |
|
 |
g Guest
|
Posted: Tue Feb 01, 2005 2:40 am Post subject: Re: calling FreeAndNil on an interface |
|
|
In the book, "Delphi COM programming", contains a thorough description on delphi interface.
MyIntf := Nil is alright
"George Birbilis" <birbilis (AT) kagi (DOT) com> wrote:
| Quote: | on D7, it seems that calling FreeAndNil on an interface type crashes the
application with access violation. If run from outside Delphi throws
infinite number of error windows till you kill the app
if anyone can reproduce it on latest Delphi, please fill-in bug report (too
busy at the moment and afraid I'll forget to do it myself later on)
//---------------------------
unit _3impact_EditorManager;
interface
type
T3ImpactObjectEditorManager=class(TInterfacedObject,I3ImpactObjectEditorManager) //...end;var defaultEditorManager:I3ImpactObjectEditorManager;implementation//...initialization defaultEditorManager:=T3ImpactObjectEditorManager.Create;finalization defaultEditorManager:=nil;//never call FreeAndNil on an interface reference,//just set it to "nil" to release the respective objectend.//---------------------------if at the "finalization" section above you do FreeAndNil(defaultEditorManager)doom happens
(and call stack window shows nothing - had to put a break pointat one of the "finalization" sections in one of my units and then move stepby step all through all actions Delphi does at the app closing till foundwhere it was crashing the app)-----George Birbilis (birbilis (AT) kagi (DOT) com)http://www.kagi.com/birbilis--------------
|
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|