Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2007-02

Assigning a virtual class method to a method pointer

1 message · 1 person ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Joao Morais

Hello,

I need to check if a virtual class method was implemented in an specific subclass. I am assigning two methods to two method pointers, casting to tmethod and comparing its code fields. The application raises a segmentation fault in this line:

     vmethodptr2 := @tbaseclassref(classparent).classmethod;

The following code raises the exception using both 2.0.4 and 2.1.1.

====
{$mode objfpc}
program testclass;
type
   tbaseclassref = class of tbaseclass;
   tbaseclass = class(tobject)
   public
     class function classmethod: string; virtual;
     class function implementsclassmethod: boolean;
   end;
   tinheritedyesclass = class(tbaseclass)
   public
     class function classmethod: string; override;
   end;
   tinheritednoclass = class(tbaseclass)
   end;
class function tbaseclass.implementsclassmethod: boolean;
var
   vmethodptr1, vmethodptr2: function: string of object;
begin
   if self  tbaseclass then
   begin
     vmethodptr1 := @classmethod;
     // here is the problem
     vmethodptr2 := @tbaseclassref(classparent).classmethod;
     result := tmethod(vmethodptr1).code  tmethod(vmethodptr2).code;
   end else
     result := false;
end;
class function tbaseclass.classmethod: string;
begin
   result := '';
end;
class function tinheritedyesclass.classmethod: string;
begin
   result := 'some stuff';
end;
const
   cboolvalues: array[boolean] of string = ('no', 'yes');
begin
   writeln(cboolvalues[tinheritedyesclass.implementsclassmethod]);
   writeln(cboolvalues[tinheritednoclass.implementsclassmethod]);
end.
====

Am I doing something wrong or this is a bug?

Thanks,

Joao Morais