23

我按顺序对控件进行了子类化,这样我就可以添加一些我需要的字段,但是现在当我在运行时创建它时,我得到一个Access Violation. 不幸的是,这种访问冲突并没有发生在我创建控件的地方,即使是我在启用所有调试选项(包括“使用调试 DCU 构建”)的情况下构建的那些,堆栈跟踪也对我没有帮助!

在我尝试重现该错误时,我尝试创建控制台应用程序,但显然此错误仅显示在 Forms 应用程序中,并且仅当我的控件实际显示在表单上时!

以下是重现错误的步骤。创建一个新的 VCL Forms 应用程序,放下一个按钮,双击以创建 OnClick 处理程序并编写以下代码:

type TWinControl<T,K,W> = class(TWinControl);

procedure TForm3.Button1Click(Sender: TObject);
begin
  with TWinControl<TWinControl, TWinControl, TWinControl>.Create(Self) do
  begin
    Parent := Self;
  end;
end;

每次我尝试时,这都会连续产生访问冲突。仅在 Delphi 2010 上对此进行了测试,因为这是我在这台计算机上获得的唯一版本。

问题是:

  • 这是德尔福泛型中的一个已知错误吗?
  • 有解决方法吗?

编辑

这是 QC 报告的链接:http: //qc.embarcadero.com/wc/qcmain.aspx?d= 112101

4

1 回答 1

27

首先,这与泛型无关,但在使用泛型时更有可能表现出来。原来有一个缓冲区溢出错误TControl.CreateParams。如果您查看代码,您会注意到它填充了一个TCreateParams结构,尤其重要的是,它TCreateParams.WinClassName使用当前类的名称(ClassName)填充 。不幸WinClassName的是,只有 char 的固定长度缓冲区64,但这需要包括 NULL 终止符;如此有效地64char longClassName将溢出该缓冲区!

可以使用以下代码对其进行测试:

TLongWinControlClassName4567890123456789012345678901234567891234 = class(TWinControl)
end;

procedure TForm3.Button1Click(Sender: TObject);
begin
  with TLongWinControlClassName4567890123456789012345678901234567891234.Create(Self) do
  begin
    Parent := Self;
  end;
end;

该类名的长度正好是64 个字符。缩短一个字符,错误就会消失!

由于Delphi 构造 的方式,在使用泛型时可能发生这种情况ClassName:它包括声明参数类型的单元名称,加上一个点,然后是参数类型的名称。例如,TWinControl<TWinControl, TWinControl, TWinControl>该类具有以下 ClassName:

TWinControl<Controls.TWinControl,Controls.TWinControl,Controls.TWinControl>

那是75字符长,超过了63限制。

解决方法

我采用了来自潜在错误生成类的简单错误消息。像这样的东西,来自构造函数:

constructor TWinControl<T, K, W>.Create(aOwner: TComponent);
begin
  {$IFOPT D+}
  if Length(ClassName) > 63 then raise Exception.Create('The resulting ClassName is too    long: ' + ClassName);
  {$ENDIF}
  inherited;
end;

至少这显示了一个可以立即采取行动的体面的错误消息。

稍后编辑,真正的解决方法

先前的解决方案(引发错误)对于具有非常长的名称的非泛型类工作正常;人们很可能能够缩短它,使其 63 个字符或更少。泛型类型并非如此:我遇到了一个带有 2 个类型参数的 TWinControl 后代的问题,所以它的形式如下:

TMyControlName<Type1, Type2>

基于此泛型类型的具体类型的 gnerate ClassName 采用以下形式:

TMyControlName<UnitName1.Type1,UnitName2.Type2>

所以它包括 5 个标识符(2x 单元标识符 + 3x 类型标识符)+ 5 个符号(<.,.>);这 5 个标识符的平均长度必须小于 12 个字符,否则总长度超过 63:5x12+5 = 65。每个标识符仅使用 11-12 个字符是非常少的,并且违反了最佳实践(即:使用长描述性名称,因为击键是免费的!)。同样,在我的情况下,我根本无法让我的标识符那么短。

考虑到如何缩短ClassName并非总是可能的,我想我会尝试消除问题的原因(缓冲区溢出)。不幸的是,这非常困难,因为错误源自层次结构TWinControl.CreateParams底部的 , 。CreateParams我们不能不调用inherited,因为CreateParams整个继承链都使用它来构建窗口创建参数。不调用它需要复制基础中的所有代码TWinControl.CreateParams加上中间类中的所有代码;它也不是很便携,因为任何代码都可能随着未来版本的VCL(或我们可能继承的 3rd 方控件的未来版本)而改变。

以下解决方案不会停止TWinControl.CreateParams溢出缓冲区,而是使其无害,然后(当inherited调用返回时)解决问题。我正在使用一个新记录(因此我可以控制布局),其中包括原始记录TCreateParams,但用大量空间填充它TWinControl.CreateParams以溢出。TWinControl.CreateParams溢出它想要的所有内容,然后我阅读完整的文本并使其符合记录的原始边界,同时确保生成的缩短名称合理地可能是唯一的。我在 WndName 中包含原始 ClassName 的 HASH 以帮助解决唯一性问题:

type
  TWrappedCreateParamsRecord = record
    Orignial: TCreateParams;
    SpaceForCreateParamsToSafelyOverflow: array[0..2047] of Char;
  end;

procedure TExtraExtraLongWinControlDescendantClassName_0123456789_0123456789_0123456789_0123456789.CreateParams(var Params: TCreateParams);
var Wrapp: TWrappedCreateParamsRecord;
    Hashcode: Integer;
    HashStr: string;
begin
  // Do I need to take special care?
  if Length(ClassName) >= Length(Params.WinClassName) then
    begin
      // Letting the code go through will cause an Access Violation because of the
      // Buffer Overflow in TWinControl.CreateParams; Yet we do need to let the
      // inherited call go through, or else parent classes don't get the chance
      // to manipulate the Params structure. Since we can't fix the root cause (we
      // can't stop TWinControl.CreateParams from overflowing), let's make sure the
      // overflow will be harmless.
      ZeroMemory(@Wrapp, SizeOf(Wrapp));
      Move(Params, Wrapp.Orignial, SizeOf(TCreateParams));
      // Call the original CreateParams; It'll still overflow, but it'll probably be hurmless since we just
      // padded the orginal data structure with a substantial ammount of space.
      inherited CreateParams(Wrapp.Orignial);
      // The data needs to move back into the "Params" structure, but before we can do that
      // we should FIX the overflown buffer. We can't simply trunc it to 64, and we don't want
      // the overhead of keeping track of all the variants of this class we might encounter.
      // Note: Think of GENERIC classes, where you write this code once, but there might
      // be many-many different ClassNames at runtime!
      //
      // My idea is to FIX this by keeping as much of the original name as possible, but
      // including the HASH value of the full name into the window name; If the HASH function
      // is any good then the resulting name as a very high probability of being Unique. We'll
      // use the default Hash function used for Delphi's generics.
      HashCode := TEqualityComparer<string>.Default.GetHashCode(PChar(@Wrapp.Orignial.WinClassName));
      HashStr := IntToHex(HashCode, 8);
      Move(HashStr[1], Wrapp.Orignial.WinClassName[High(Wrapp.Orignial.WinClassName)-8], 8*SizeOf(Char));
      Wrapp.Orignial.WinClassName[High(Wrapp.Orignial.WinClassName)] := #0;
      // Move the TCreateParams record back were we've got it from
      Move(Wrapp.Orignial, Params, SizeOf(TCreateParams));
    end
  else
    inherited;
end;
于 2013-01-21T21:03:57.103 回答