I subclassed a control in order so I can add a few fields that I need, but now when I create it at runtime I get an Access Violation. Unfortunately this Access Violation doesn’t happen at the place where I’m creating the control, and even those I’m building with all debug options enabled (including "Build with debug DCU’s") the stack trace doesn’t help me at all!
In my attempt to reproduce the error I tried creating a console application, but apparently this error only shows up in a Forms application, and only if my control is actually shown on a form!
Here are the steps to reproduce the error. Create a new VCL Forms application, drop a single button, double-click to create the OnClick handler and write this:
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;
This successively generates the Access Violation, every time I tried. Only tested this on Delphi 2010 as that’s the only version I’ve got on this computer.
The questions would be:
- Is this a known bug in Delphi’s Generics?
- Is there a workaround for this?
Edit
Here’s the link to the QC report: http://qc.embarcadero.com/wc/qcmain.aspx?d=112101
First of all, this has nothing to do with generics, but is a lot more likely to manifest when generics are being used. It turns out there’s a buffer overflow bug in
TControl.CreateParams. If you look at the code, you’ll notice it fills aTCreateParamsstructure, and especially important, it fills theTCreateParams.WinClassNamewith the name of the current class (theClassName). UnfortunatelyWinClassNameis a fixed length buffer of only64char’s, but that needs to include the NULL-terminator; so effectively a64char longClassNamewill overflow that buffer!It can be tested with this code:
That class name is exactly 64 characters long. Make it one character shorter and the error goes away!
This is a lot more likely to happen when using generics because of the way Delphi constructs the
ClassName: it includes the unit name where the parameter type is declared, plus a dot, then the name of the parameter type. For example, theTWinControl<TWinControl, TWinControl, TWinControl>class has the following ClassName:That’s
75characters long, over the63limit.Workaround
I adopted a simple error message from the potentially-error-generating class. Something like this, from the constructor:
At least this shows a decent error message that one can immediately act upon.
Later Edit, True Workaround
The previous solution (raising an error) works fine for a non-generic class that has a realy-realy long name; One would very likely be able to shorten it, make it 63 chars or less. That’s not the case with generic types: I ran into this problem with a TWinControl descendant that took 2 type parameters, so it was of the form:
The gnerate ClassName for a concrete type based on this generic type takes the form:
so it includes 5 identifiers (2x unit identifier + 3x type identifier) + 5 symbols (
<.,.>); The average length of those 5 identifiers need to be less then 12 chars each, or else the total length is over 63: 5×12+5 = 65. Using only 11-12 characters per identifier is very little and goes against best practices (ie: use long descriptive names because keystrokes are free!). Again, in my case, I simply couldn’t make my identifiers that short.Considering how shortening the
ClassNameis not always possible, I figured I’d attempt removing the cause of the problem (the buffer overflow). Unfortunately that’s very difficult because the error originates fromTWinControl.CreateParams, at the bottom of theCreateParamshierarchy. We can’t NOT callinheritedbecauseCreateParamsis used all along the inheritance chain to build the window creation parameters. Not calling it would require duplicating all the code in the baseTWinControl.CreateParamsPLUS all the code in intermediary classes; It would also not be very portable, since any of that code might change with future versions of theVCL(or future version of 3rd party controls we might be subclassing).The following solution doesn’t stop
TWinControl.CreateParamsfrom overflowing the buffer, but makes it harmless and then (when theinheritedcall returns) fixes the problem. I’m using a new record (so I have control over the layout) that includes the originalTCreateParamsbut pads it with lots of space forTWinControl.CreateParamsto overflow into.TWinControl.CreateParamsoverflows all it wants, I then read the complete text and make it so it fits the original bounds of the record also making sure the resulting shortened name is reasonably likely to be unique. I’m including the a HASH of the original ClassName in the WndName to help with the uniqueness issue: