JCL Help:Guard@Pointer@IMultiSafeGuard - Project JEDI Wiki

JCL Help:Guard@Pointer@IMultiSafeGuard

From Project JEDI Wiki

Jump to: navigation, search


[edit] Summary

Associates a resource, pointer or object reference, with a safeguard.

[edit] Pascal

 function Guard(Mem: Pointer; out SafeGuard: ISafeGuard): Pointer; overload;
function Guard(Obj: TObject; out SafeGuard: ISafeGuard): TObject; overload;
function Guard(Mem: Pointer; var SafeGuard: IMultiSafeGuard): Pointer; overload;
function Guard(Obj: TObject; var SafeGuard: IMultiSafeGuard): TObject; overload;

[edit] Parameters

Parameters Description
Mem: Pointer The pointer (or object in the case of the overloaded Guard function) which to associate with the safeguard.
out SafeGuard: ISafeGuard The guard variable to associate the resource with.

[edit] Return Value

Copy of the Mem (or Obj) parameter. This is merely a convenience which allows you to create the resource embedded inside the function call as demonstrated above.

[edit] Description

Associates a resource, pointer or object reference, with a safeguard. A safeguard is an interface which gets associated with the resource and ensures that when execution leaves the scope at which the safeguard variable is declared, the resource is released (e.g. memory freed or object 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: ISafeGuard; Strings: TStrings; begin Strings := TStrings(Guard(TStringList.Create, SafeGuard)); String.ReadFromFile('d:delphijclsourceJclBase.pas'); // code to manipulate strings goes here Strings.SaveToFile('d:delphijclsourceJclBase.pas'); end;
In the above code, the TStrings object is associated with the SafeGuard. No matter how the function terminates, the safeguard ensures that the TStrings object is 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 the ISafeGuard interface for more information.

[edit] See Also

GuardGetMem GuardAllocMem ISafeGuard

[edit] About

[edit] Unit


[edit] Donator

Rudy Velthuis

[edit] 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

Personal tools
Project JEDI
Code Library
Visual Component Library
Version Control System
Math Library
Code Formatter