使用Delphi

时间:2015-11-23 02:18:34

标签: delphi

以下代码尝试使用Delphi的引用计数功能。

但是,FullDebugMode报告中的FastMM4 DoStuff1会导致内存泄漏,而DoStuff2则不会。你能帮忙评论一下为什么吗?这两个程序不应该在幕后表现完全相同吗?

program Project_SO;

{$APPTYPE CONSOLE}

uses
  FastMM4, 
  SysUtils;

type  
  ITestFunc = interface
  ['{B3F6D9A7-FC77-40CE-9BBF-C42D7037A596}']
    function DoIt(X,Y: Integer): Integer;
  end;

  TTestFunc = class(TInterfacedObject, ITestFunc)
  public
    function DoIt(X,Y: Integer): Integer;
  end;
  TTestFuncClass = class of TTestFunc;   

{ TTestFunc }

function TTestFunc.DoIt(X, Y: Integer): Integer;
begin
  Result := X + Y; 
end;

function DoStuff1(Num1, Num2: Integer; OperationClass: TTestFuncClass): Integer;
begin
  Result := ITestFunc(OperationClass.Create).DoIt(Num1, Num2);
end;

function DoStuff2(Num1, Num2: Integer; OperationClass: TTestFuncClass): Integer;
var I: ITestFunc;
begin
  I := ITestFunc(OperationClass.Create);
  Result := I.DoIt(Num1, Num2);
end;

begin
  Writeln(IntToStr(DoStuff1(3, 6, TTestFunc)));
  Writeln(IntToStr(DoStuff2(3, 6, TTestFunc)));
end.

0 个答案:

没有答案