[Lazarus] Weird object variables access [SOLVED] - Sorry for the noise!

Giuliano Colla giuliano.colla at fastwebnet.it
Fri Nov 3 18:20:27 CET 2017

Il 25/10/2017 14:12, Giuliano Colla via Lazarus ha scritto:

I raised some noise about procedural variables in Delphi mode, but it 
turns out that I had failed to notice some discrepancies while porting 
code back and forth from Delphi to Lazarus.


    I had failed to notice that RAD Studio X10 when importing the Delphi
    7 project had silently dropped a declaration from my source, and
    this is what gave rise to a compiler error.


    I had failed to detect that the OnCreate event, which should have
    properly initialized the procedural variable in my test program was
    never triggered.

When I realized it I did refine and repeat all my tests in Kylix, Delphi 
7, X10 and fpc, and the final result is the following:

1) Delphi accepts the assignment of an untyped pointer to a procedural 
variable even if it is a method pointer. This is true from Delphi 3 up 
to Kylix, Delphi 7 and Rad Studio X10.

2) The syntax for this particular assignment (again from Delphi 3 to 
X10) is:

@Procvar := aPointer;

3) What Delphi does in that case is:
copy the pointer to the procedure in the "code" field of the procedural 
variable, leaving the "data" field untouched.

4) fpc in Delphi mode does exactly the same.

This makes it possible in Delphi, or in Delphi mode fpc, to initialize 
the procedural variable to point to a method of an object, and 
subsequently to make it point to any other compatible method of the same 
object, just by providing an untyped pointer, which may be handled with 
the many tools which deal only with pointers, for jump tables, queues of 
actions etc.

Of course this is not the best thing to do. An appropriate typecast of 
the procedural variable to a TMethod, and then explicitly setting the 
fields would make the code much more readable. That is what common sense 
and good programming practice would suggest. ObjFpc mode only provides 
this second option, for the same purpose, and this is certainly better.

But, as the purpose of Delphi mode is not to provide what common sense 
or good programming practice would suggest, but to mimic Delphi 
behaviour, I raised the question when I detected an apparent fpc bug.

It turns out that it was my fault, so, sorry for the noise.


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus-ide.org/pipermail/lazarus/attachments/20171103/737b9934/attachment.html>

More information about the Lazarus mailing list