Yes, it saves. Your code
function Example.Foo: Integer; var lock : ILock; begin lock := ScopedLock<TCriticalSection>.Create(mySync); // ... end;
compiled as following pseudocode
function Example.Foo: Integer; var lock : ILock; begin lock := ScopedLock<TCriticalSection>.Create(mySync); lock._AddRef; // ref count = 1 try // .. finally lock._Release; // ref count = 0, free lock object end;
You can see that when the var lock goes out of scope, its number of links decreases, becomes zero, and the lock object is automatically destroyed.
kludg source share