I recently came across a problem caused by some very old code that I wrote, which obviously assumed that the interface references used in the with statement would be released as soon as with -block remained - kind of like an implicit try-finally -block (similar to C # using -statement, if I understood correctly).
Apparently (in Delphi 2009) this is not (no more?) A case. Does anyone know when this happened? Or was my code just wrong for a start?
To clarify, here is a simplified example:
type IMyIntf = interface; TSomeObject = class(TInterfacedObject, IMyIntf) protected constructor Create; override; // creates some sort of context destructor Destroy; override; // cleans up the context created in Create public class function GetMyIntf: IMyIntf; //a factory method, calling the constructor end; procedure TestIt; begin DoSomething; with (TSomeObject.GetMyIntf) do begin DoStuff; DoMoreStuff; end; // <- expected: TSomeObject gets destroyed because its ref.count is decreased to 0 DoSomethingElse; end; // <- this is where TSomeObject.Destroy actually gets called
Whenever someone started the old argument with is evil, it was always one of the examples that I had in mind that made me go βYes, but ...β. I think I was wrong ... Can anyone confirm?
source share