Okay,
from the RTFC school,
the 3 ways
  Nahums original (DoThisStuff2  below),
  Leighs (DoThisStuff1  below),
  and Nevens number 2 (DoThisStuff3  below)
all have the same result.

Think I've missed something somewhere, but no
matter what order I do things in, 'issues' is
*always* created.

Theres some dodgy-ish stuff in here, but it should highlight the issues.

Here is the output:


Error THardToFind Changes = nil terrorists = nil issues = 4847180 Error THardToFind Changes = nil terrorists = nil issues = 4847180 Error THardToFind Changes = nil terrorists = nil issues = 4847180

From :

//=================================================================
procedure TForm5.Log(s : String);
begin
  Memo1.Lines.Add(s);
end;

procedure TForm5.test(f : testfn);
begin
  Try
    if f then
      Log('done')
    else
      Log('not done');
  except on E:Exception do
    Log('Error ' + e.message);
  end;

  if Unit4.Changes = nil then Log('Changes = nil')
  else Log('Changes = ' + IntToStr(integer(Unit4.Changes)));

  if Unit4.terrorists = nil then Log('terrorists = nil')
  else Log('terrorists = ' + IntToStr(integer(Unit4.terrorists)));

  if Unit4.tIssuesList = nil then Log('issues = nil')
  else Log('issues = ' + IntToStr(integer(Unit4.tIssuesList)));
end;

procedure TForm5.Button3Click(Sender: TObject);
begin
  test(Unit4.DoThisStuff3);
  test(Unit4.DoThisStuff2);
  test(Unit4.DoThisStuff1);
end;
//=================================================================

where :

//=================================================================
unit Unit4;

interface
uses SysUtils;

type
  testfn = function : Boolean;

  tIssuesList = class(TObject)
    constructor Create;
  end;
  THardToFind = class(TObject)
    constructor Create;
  end;
  tchanges  = class(TObject)
    constructor Create;
  end;
  function DoThisStuff1 : Boolean;
  function DoThisStuff2 : Boolean;
  function DoThisStuff3 : Boolean;
var
  Changes : TChanges;
  Issues  : tIssuesList;
  terrorists : THardToFind;
implementation

constructor tchanges.Create;
begin
  raise Exception.create('tChanges');
end;
constructor tIssuesList.Create;
begin
  raise Exception.create('tIssues');
end;
constructor THardToFind.Create;
begin
  raise Exception.create('THardToFind');
end;

function DoThisStuff1 : Boolean;
begin
  Result := False;
  terrorists := THardToFind.create;
  try
    changes := tchanges.create;
    try
      issues := tIssuesList.create;
      try
        Result := False;
      finally
        issues.free;
      end;
    finally
      changes.free;
    end;
  finally
    terrorists.free;
  end;
end;

function DoThisStuff2 : Boolean;
begin
  Result := False;
  terrorists := THardToFind.create;
  changes := tchanges.create;
  issues := tIssuesList.create;
  try
        Result := False;
  finally
    issues.free;
    changes.free;
    terrorists.free;
  end;
end;

function DoThisStuff3 : Boolean;
begin
  Result := False;
  terrorists := nil;
  changes := nil;
  issues := nil;
  try
    terrorists := THardToFind.create;
    changes := tchanges.create;
    issues := tIssuesList.create;
    Result := True;
  finally
    FreeAndNil(terrorists);
    FreeAndNil(changes);
    FreeAndNil(issues);
  end

end;

end.
//=================================================================

_______________________________________________
Delphi mailing list
[EMAIL PROTECTED]
http://ns3.123.co.nz/mailman/listinfo/delphi

Reply via email to