[fpc-devel] Optimization breaks check for overridden function / Is there a better way to check?

David Jenkins david at scootersoftware.com
Fri Oct 17 19:35:17 CEST 2025


What is the best way to check if a virtual function in a base class has 
been overridden by a class that inherits the base class?

In LCL TPrinter.pas the following check is used:


procedure TPrinter.NewPage;
begin
   Inc(fPageNumber);
   if TMethod(@Self.DoNewPage).Code = Pointer(@TPrinter.DoNewPage) then
     begin
     ..
     end
  ..
end

We have run across an optimization problem with the TCocoaPrinter class 
and that TPrinter code.

TCocoaPrinter = class(TPrinter)

with no override of DeNewPage

Sometime with Level2 or Level3 optimization, 
TMethod(@Self.DoNewPage).Code is changed to point to systemh.inc 
EmptyMethod instead of TPrinter.DoNewPage and the check no longer works 
appropriately.

Is there another way to make that check that is not affected by 
optimization?

I will eventually throw a fix to the LCL folks but we use a similar 
formulation in our code and are concerned that it might also be affected 
by optimization at some point.   Right now it is not because none of the 
base functions are completely empty, but they are trivial functions that 
match each other and could be optimized in the future by coalescing them.

Thanks
David Jenkins
Scooter Software.




More information about the fpc-devel mailing list