[fpc-devel] Simple cast compiled as AS with -CR
Jonas Maebe
jonas at freepascal.org
Fri Dec 30 11:27:44 CET 2016
On 2016-12-30 10:45, Ondrej Pokorny wrote:
> var
> O: TMyClass1;
> begin
> O := TMyClass1.Create;
> TMyClass2(O).Free; // << 219-runtime error if compiled with -CR
> end.
>
> If I compile it with "-CR", I get a 219-runtime error when casting O
> to TMyClass2. Is this wanted/documented? I see that {$OBJECTCHECKS
> OFF} fixes it.
Yes, this is by design. The above code is invalid and relies on
implementation details to work. E.g. with a JVM or CIL backend, the code
would be rejected by the bytecode verifier too.
> Why I ask: this hack is used in the LCL to access protected methods -
> i.e. the LCL doesn't work correctly with -CR.
Class crackers are an unsupported hack. In case you access fields, it
can also cause other problems:
http://wiki.freepascal.org/FPC_New_Features_3.0#Class_field_reordering .
In the future, more such situations may arise.
The compiler is supposed to be able to rely on the fact that the type
hierarchy you declare is in fact the real type hierarchy, and not just a
collection of statements that you use like a macro-assembler with the
expectation of ending up with a particular memory layout.
Jonas
More information about the fpc-devel
mailing list