Virtual Pascal

Classic Object Pascal for free

Bug in VPC - Program crash when Method references are passed to parameters or variables

Hi Folks

I am using VPC for years and it is still my favorite compiler due to its powerful debugging capabilities. Nevertheless VPC suffers from a severe bug. I found it years ago, developed a work-around for it but recently I suffered again from the same bug.

PROBLEM: When the reference to a Method is passed as parameter to another routine or assigned to a variable then VPC will crash!
For instance if TestStatic is a procedure method of MyObject and MyMethod is a variable of the type procedure(X:Integer) of object;  then the following valid assignment will cause VPC to crash!


  MyMethod:=MyObject.TestStatic;  //this fails and let it crash lateron
  MyMethod(3);                    //this looks OK but the wrong MyObject is passed


The reason of the crash is that the compiler generates invalid code. In one place the code generator of VPC allocates register EAX where it should have allocated register ECX (or reverse) and in another place it pushes something on the stack where it is never popped-off.

This bug is in Virtual Pascal verion 2.1, build 279, from may 2004.



PROOF: The program that I have attached illustrates this bug and shows a few work-arounds as well
A much better solution would be a bug-fix in VPC: the code generator allocates at some point register ECX in stead of EAX. This is probably a very simple fix but I think that is only in the hands of Allen Mertner ....

Although passing methods to other routines is a pretty obvious programming practice, it happened that it was never used in any of the libraries of VPC. That's why the bug was not discovered by compiling and testing those libraries.

WORKAROUND: I developed a work-around to make the above example work in practice. It uses two pointers to the elements of a full method reference. One pointer should receive the memory address of the routine and the other one a pointer to the object to which this routine belongs.  

  AssignMethod(MyMethod, @TMyObject.TestStatic, @MyObject); 
  MyMethod(3); 

Procedure AssignMethod is doing the job for you, you can find it in the attached program and also how to use it.

This "solution" works, it is NOT elegant, but it is better than nothing at all.

Allen Mertner, it would be great if you can fix it in the compiler and create a new build.......... (yes I know)

Regards, Rob

 

Views: 86

Attachments:

Reply to This

© 2022   Created by Allan Mertner.   Powered by

Report an Issue  |  Terms of Service