小编mal*_*lom的帖子

为什么提出异常并不仅限于异常类?

这个delphi代码是可编译的,并按预期工作.

program RaiseLabel;

uses FastMM4, Windows, StdCtrls;

function CreateLabel(const S: string): TLabel;
begin
  Result := TLabel.Create(nil);
  Result.Caption := S;
end;

begin
  try
    raise CreateLabel('Strange exception example');
  except
    on L: TLabel do begin
      MessageBox(0, PChar(L.Caption), 'TLabel', MB_OK);
    end;
  end;
end.
Run Code Online (Sandbox Code Playgroud)

我的问题是,为什么异常处理不仅限于引发异常对象,它是异常类的后代.(我在完全调试模式下使用FastMM来测试此程序是否会导致任何内存泄漏 - 但事实并非如此.)

delphi exception-handling

6
推荐指数
0
解决办法
111
查看次数

将记录复制到动态分配的内存时,Delphi 7引用计数错误

在将带有托管字符串字段的记录类型变量分配给动态分配的缓冲区时,我在Delphi中遇到了一种奇怪的行为.这有什么问题,如何纠正?

type
  PRec = ^TRec;
  TRec = packed record
    Foo: integer;
    Bar: string;
  end;

procedure Error;
var
  P, Q: PRec;
  R, T: TRec;
begin
  R.Foo := 1;
  R.Bar := 'Bar';
  T := R; // Ready
  Q := @T;
  Q^ := R; // Ready
  GetMem(P, SizeOf(TRec));
  P^ := R; // Access violation in _LStrAsg at 
           // "MOV     ECX,[EDX-skew].StrRec.refCnt"

  R := P^; // Just to keep reference while debugging
end;
Run Code Online (Sandbox Code Playgroud)

delphi automatic-ref-counting

4
推荐指数
1
解决办法
128
查看次数

使用泛型的 Delphi 属性 getter 函数

拥有通用属性 getter/setter 来对每次访问执行常见任务会很好。

该代码在 Delphi XE2 'E2008 Incompatible types' 中给出了一个编译时错误。类似的代码在编译期间给出了一个内部错误,但从未编译过。我犯了错误还是编译器限制?

type TFoo = class
private
  function Get<T>: T;
public
  property Bar: Integer read Get<Integer>;
end;

function TFoo.Get<T>: T;
begin
  Result := 0;
end;
Run Code Online (Sandbox Code Playgroud)

delphi generics properties delphi-xe2

3
推荐指数
1
解决办法
177
查看次数