3

我正在使用其创建者不再支持的旧脚本引擎,并且在内存泄漏方面遇到了一些麻烦。它使用 ASM 编写的函数从脚本调用 Delphi 函数,并将结果作为整数返回,然后将该整数作为无类型参数传递给另一个过程,该过程将其转换为正确的类型。

这对大多数事情都很好,但是当 Delphi 函数的返回类型是 Variant 时,它会泄漏内存,因为该变量永远不会被处理掉。有谁知道我如何获取包含变体的无类型参数并确保正确处理它?这可能会涉及一些内联汇编。

procedure ConvertVariant(var input; var output: variant);
begin
  output := variant(input);
  asm
    //what do I put here? Input is still held in EAX at this point.
  end;
end;

编辑:在评论中回应 Rob Kennedy 的问题:

AnsiString 转换的工作方式如下:

procedure VarFromString2(var s : AnsiString; var v : Variant);
begin
  v := s;
  s := '';
end;

procedure StringToVar(var p; var v : Variant);
begin
  asm
    call VarFromString2
  end;
end;

这工作正常,不会产生内存泄漏。当我尝试使用变体作为输入参数做同样的事情,并Null在第二个过程中分配原始参数时,内存泄漏仍然发生。

变体主要包含字符串——所讨论的脚本用于生成 XML——并且它们通过将 Delphi 字符串分配给该脚本正在调用的 Delphi 函数中的变体来到达那里。(在这种情况下,更改函数的返回类型不起作用。)

4

1 回答 1

3

Have you tried the same trick as with the string, except that with a Variant, you should put UnAssigned instead of Null to free it, like you did s := ''; for the string.

And by the way, one of the only reasons I can think of that requires to explicitly free the strings, Variants, etc... is when using some ThreadVar.

于 2010-09-03T21:03:50.643 回答