9
votes

I'm trying to have RTTI enabled only for a subset of my classes.

The reason is that for those classes for which I want RTTI, I want RTTI on public methods too, but if that is enabled project-wide, then all public methods from all classes get into the final executable. This basically turns off the smart-linking, as the compiler considers that every public method could be called at runtime, and thus ends up compiling pretty much everything and the kitchen sink into the executable...

I've tried several things:

  • Turning off RTTI at the project level with {$RTTI EXPLICIT METHODS([]) PROPERTIES([]) FIELDS([])} and then re-enabling it for the relevant units results in crashes at compile time (an AV somewhere in the compiler) on the $RTTI directive.
  • Turning off RTTI at the project level and then enabling it class-by-class compiles, but at runtime it results in an unqualified AV deep in "Rtti.pas" when attempting to access the RTII for the exposed classes
  • Controling RTTI via $RTTI directives embedded in the ".inc" all units use results in random AV at compile time (always at the line of the $RTTI directive, but not always for the same unit).

Any other ideas?

1
As far as I know, the accepted way of doing this is to have a TMyCustomClass (with no published items at all) and then a TMyClass with ONLY a published section listing the items to be made visible. The VCL does this a lot (TCustomForm / TForm etc).Brian Frost
I'm in a somewhat different case, f.i. you can see it happening in the DWScript's unit tests: I want RTTI exposed for the classes used in the expose tests, but not for everything else. Without such a filter, an RTTI-based exposition of classes becomes problematic, as it becomes a binary choice between exposing everything, or not using RTTI at all to avoid exposing everything...Eric Grange
What you are trying to do and the approaches you have tried appear fine to me. Sounds like a compiler or "RTTI design" bug.Marjan Venema
@Brian: what you are describing is just a way to change visibility of properties so that the TMyClass will show its properties in the Object Inspector, while TMyCustomClass can be used to derive your own classes from without the "burden" of published properties. (In the old RTTI style, only published properties had RTTI).Marjan Venema
QC finally responsive, added as 98261Eric Grange

1 Answers

1
votes

Compiler bug submitted as QC 98261 for Embarcadero consideration.

The runtime AV was related to attributes, so a workaround is to make sure (manually, there are no compiler errors or warnings) that the attributes used in the exposed classes all have RTTI for them, otherwise you get the unqualified runtime AV.

The compiler AV happens whenever the $RTTI directive is used before the "unit" statement of a unit, if you place it after the AV doesn't happen and it works.