Error with static objects after assignment: sizeof delivers wrong result and dynamic method call is used
Original Reporter info from Mantis: Heinz.Dobler
-
Reporter name: Heinz Dobler
Original Reporter info from Mantis: Heinz.Dobler
- Reporter name: Heinz Dobler
Description:
Assume the following two types A and B:
TYPE
A = OBJECT
i: INTEGER;
CONSTRUCTOR Init(i_par: INTEGER);
PROCEDURE Write;
END; (*OBJECT*)
B = OBJECT(A)
r: REAL;
CONSTRUCTOR Init(i_par: INTEGER; r_par: REAL);
PROCEDURE Write;
END; (*OBJECT*)
and two variables (static objects) of these types:
VAR
sa: A;
sb: B;
where SizeOf(sa) = 8 and SizeOf(sb) = 16.
After initialization with sa.Init(1) and sb.Init(2, 3.0), the assignment
sa := sb;
has the effect that SizeOf(sa) = 16. But 8 would be the correct result.
Even worse: when the two Write methods in A and B are declared VIRTUAL (to request dynamic binding), after the assignment above, the method call
sa.Write;
calls the Write method in class B which prints a non existing value for B.r.
Steps to reproduce:
To reproduce the Errors use the uploaded program OOP_Test.
1. For the SizeOf error:
> fpc OOP_Test.pas
> OOP_Test.exe
2. For the erroneous call of the Write method:
Change line number 3 in OOP_Test.pas to
{$DEFINE VIRTUAL}
then as above:
> fpc OOP_Test.pas
> OOP_Test.exe
Mantis conversion info:
- Mantis ID: 28153
- OS: Windows
- OS Build: Windwos 7 64 Bit
- Build: 2014/03/06, i386-win32
- Platform: Intel
- Version: 2.6.4