Bonjour

avez vous testé la methode qui suit de liberation ?
(Tiré de http://www.delphi3000.com/articles/article_2888.asp?SK=

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
unit GarbageCollector;
 
interface
 
type
  ISafeGuard = type IUnknown;
 
// Use the Guard function to instantiate the object!
function Guard(Obj : TObject; out SafeGuard : ISafeGuard) : TObject;
 
implementation
 
type
  TSafeGuard = class(TInterfacedObject, ISafeGuard)
  private
    FObj : TObject;
  public
    constructor Create(Obj : TObject);
    destructor Destroy; override;
  end;
 
constructor TSafeGuard.Create(Obj : TObject);
begin
  FObj := Obj;
end;
 
destructor TSafeGuard.Destroy;
begin
  if Assigned(FObj) then
  begin
    FObj.Free;
    FObj := nil;
  end;
  inherited Destroy;
end;
 
function Guard(Obj : TObject; out SafeGuard : ISafeGuard) : TObject;
begin
  Result := Obj;
  SafeGuard := TSafeGuard.Create(Obj);  // Create the proxy obj.
end;
 
end.
Pour creer un objet on fait ainsi

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
 
 
 
 
 
  var obj : TMyObj;
      SafeGuard : ISafeGuard;
 
  obj := TMyObj(Guard(TMyObj.Create, SafeGuard));
 
 
 
 TMyObj descend  TObject
example:

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
 
 
 
{$APPTYPE CONSOLE}
program Test;
 
uses GarbageCollector;
 
{ --- Test class --- }
type
  TTest = class(TObject)
  public
    constructor Create;
    destructor Destroy; override;
 
    procedure Test;
  end;
 
{ ---- Test members --- }
constructor TTest.Create;
begin
  WriteLn('Create()');
end;
 
destructor TTest.Destroy;
begin
  WriteLn('Destroy()');
  inherited;
end;
 
procedure TTest.Test;
begin
  WriteLn('Test()');
end;
{ ---- End Of Test members --- }
 
{ ---- Test Procedure --- }
 
procedure TestProc;
var SafeGuard : ISafeGuard;
    obj : TTest;
begin
  obj := TTest(Guard(obj.Create, SafeGuard)); // Create!
  obj.Test;
  { Notice that we are not explicitlly destroying the object here! }
end;
{ ---- End Of Test Procedure --- }
 
 
begin
  TestProc;
end.
Est ce efficace ?