Getting address of TSomeObject.AfterConstruction without using PVMT lazarus / 64-FPC development ---------------------------------------------------------------------- BrunoK · 2019-02-02 09:58 In the following procedure Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i const  cCPEmptyMethod : Codepointer = nil;  cCPAfterConstruction : Codepointer = nil; function _InternalCreate(aSelf : TClass; aCreate: UIntPtr): TObject;var  lAfterConstruction : CodePointer;begin  Result := aSelf.Newinstance;  if Result = nil then    exit;  { TObject.AfterConstruction does nothing, skip try / finally block thus    reducing code overhead for 90% of classes (?) }  lAfterConstruction := PVMT(aSelf)^.vAfterConstruction;  // lAfterConstruction := @Result.AfterConstruction; // if activated, generates error  // fstobjcr.pp(96,25) Error: Incompatible types: got "" expected "Pointer"  if lAfterConstruction <> cCPAfterConstruction then    if lAfterConstruction <> cCPEmptyMethod then     try       lAfterConstruction := nil;       Result.AfterConstruction;       lAfterConstruction := Result;     finally       if lAfterConstruction = nil then begin // Failed AfterConstruction         Result.Destroy;       Result := nil;       end;     end;end; I would like to replace  PVMT(aSelf)^.vAfterConstruction with something like shown in the next line. But expressed  like in snippet line 15, it gives error like in line 16. Is there a way to get 'simply' (without having to go through complicated things that I already know how to do) a correct transtyping of lAfterConstruction := @Result.AfterConstruction. ---------------------------------------------------------------------- howardpc · 2019-02-02 10:57 The following compiles. Whether it achieves what you want, I am not sure. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i uses Forms; function _InternalCreatedOK(aSelf: TClass; out anInstance: TObject): Boolean;var  lAfterConstruction: TProcedureOfObject;begin  Result := True;  anInstance := aSelf.newinstance;  if anInstance = Nil then    Exit(False);  lAfterConstruction := @anInstance.AfterConstruction; // if activated, generates error  try  if Assigned(lAfterConstruction) then    lAfterConstruction := Nil;  anInstance.AfterConstruction;  finally    if Assigned(lAfterConstruction) then begin // Failed AfterConstruction    anInstance.Destroy;      anInstance := Nil;      Result := False;  end;  end;end; ---------------------------------------------------------------------- ASerge · 2019-02-02 11:06 May be this: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i {$MODE OBJFPC} var  GAfterConstruction: CodePointer = nil; type  TAfterContruction = procedure of object; function GetCodeOfAfterContruction(Method: TAfterContruction): CodePointer; inline;begin  Result := TMethod(Method).Code;end; function _InternalCreate(ASelf: TClass; aCreate: UIntPtr): TObject;var  LAfterConstruction: CodePointer;begin  Result := ASelf.NewInstance;  if Result = nil then    Exit;{ TObject.AfterConstruction does nothing, skip try / finally block thus  reducing code overhead for 90% of classes (?) }  LAfterConstruction := GetCodeOfAfterContruction(@Result.AfterConstruction);  if LAfterConstruction = GAfterConstruction then    // Skip default  else    Result.AfterConstruction;end; procedure InitGAfterConstruction;var  Dummy: TObject;begin  Dummy := TObject.Create;  try    GAfterConstruction := GetCodeOfAfterContruction(@Dummy.AfterConstruction);  finally    Dummy.Free;  end;end; begin  InitGAfterConstruction;end. ---------------------------------------------------------------------- BrunoK · 2019-02-02 11:49 @howardpc, @ASerge Thanks. As soon as my 3.0.6 is again functional (some little modifications beiing done), I'll test. The objective is to have as little byte code as possible, otherwise what I'm trying to do do is useless (once again ...).