JCL Help:IMultiSafeGuard

From Project JEDI Wiki
Jump to navigationJump to search

Class Hierarchy

IMultiSafeGuard
|
TJclMultiSafeGuard
|
TJclObjMultiSafeGuard


Summary

Interface used to provide automatic resource destruction for multiple resources.


Pascal

 public IMultiSafeGuard = interface(IInterface);


Description

IMultiSafeGuard is the interface used by the Guard functions to provide automatic resource destruction for multiple resources. A safeguard is an interface which gets associated with the resources and ensures that when execution leaves the scope at which the safeguard variable is declared, the resources are released (e.g. memory freed or objects destroyed). Since a safeguard is an interface and Delphi ensures that interfaces are released even when an exception is raised, you can use a safeguard to ensure that a resource always gets freed without having to explicitly code it. For example:
var SafeGuard: IMultiSafeGuard; O1, O2: TObject; begin O1 := Guard(TObject.Create, SafeGuard); O2 := Guard(TObject.Create, SafeGuard); // use O1 and O2 here end;
In the above code, the objects are associated with the SafeGuard. No matter how the function terminates, the safeguard ensures that the objects are destroyed. Usually you will simply store the returned safeguard interface pointer in a variable at the appropriate scope and forget about it. However, the ISafeGuard interface does provide some additional functionality which may be useful at times.


See Also

Guard ISafeGuard


About

Unit

JclSysUtils


Navigation

Donator

Team JEDI


Notes

You can use IMultiSafeGuard to guard multiple memory allocations or multiple objects. However, a single IMultiSafeGuard interface can only store one or the other, you can not mix memory and objects in a single IMultiSafeGuard.


Contribute to this help topic

This documentation wiki is based on the collaborative effort of Project JEDI users. Your edits are welcome in order to improve documentation quality: edit this page