Jump to content
dmitrybv

CaretPositionRTTIProp.GetValue(Memo1) raises AV

Recommended Posts

Hello

 

I can't figure out why the following code causes an AV error.


 

procedure TFormSimpleDraw2.Button2Click(Sender: TObject);
var
  Value: TValue;
  LType: TRttiType;
  RttiContext: TRttiContext;
  CaretPositionProp: TRttiProperty;
  CaretPosition: TCaretPosition;
begin
  RttiContext := TRttiContext.Create;
  LType := RttiContext.GetType(Memo1.ClassInfo);

  CaretPosition := Memo1.CaretPosition;

  CaretPositionProp := LType.GetProperty('CaretPosition');
  Value := CaretPositionProp.GetValue(Memo1); //AV is here
end;

 

Embarcadero® RAD Studio 12 Version 29.0.55362.2017 

 

1. Create FMX Form.

2. Put TButton and TMemo on the Form.

3. Write TButton.OnClick as written above

4.  Run and click Button

Share this post


Link to post

You are not checking if either TRttiContext.GetType() or TRttiType.GetProperty() are returning nil before accessing the objects they return.

procedure TFormSimpleDraw2.Button2Click(Sender: TObject);
var
  Value: TValue;
  LType: TRttiType;
  RttiContext: TRttiContext;
  CaretPositionProp: TRttiProperty;
  CaretPosition: TCaretPosition;
begin
  RttiContext := TRttiContext.Create;

  LType := RttiContext.GetType(Memo1.ClassInfo);
  if LType = nil then Exit; // <-- add this

  CaretPosition := Memo1.CaretPosition;

  CaretPositionProp := LType.GetProperty('CaretPosition');
  if CaretPositionProp = nil then Exit; // <-- add this
  
  Value := CaretPositionProp.GetValue(Memo1);
  ...
end;

Share this post


Link to post

CaretPositionProp is not nil, however when invoking the GetValyue in CaretPositionProp.GetValue self is an Inaccessible value:

 

FMX.Memo.TCustomMemo.GetCaretPosition
:00cfcac8 RawInvoke + $40
System.Rtti.RawInvoke(???,???)
:00cfcead Invoke + $249
System.Rtti.Invoke(nil,(),???,???,True,True)
:00cf5689 TRttiInstanceProperty.DoGetValue + $DD
System.Rtti.TRttiInstanceProperty.DoGetValue(???)
:00cf54ae TRttiProperty.GetValue + $26

 

Since it is pointing to an invalid address the Access Violation is when the code tries to execute Model.CaretPosition.

Edited by Lajos Juhász

Share this post


Link to post

If this issue is related to a bug in RTTI or TMemo, then a new bug report should be added to the Embarcadero Quality Portal.


If this issue is related to the fact that the CaretPosition property cannot be read via RTTI, then is there a way to check this.

Share this post


Link to post

Okay, the reason is because the CaretPosition getter is declared as cdecl (I have no clue why that is the case - I would assume some C++ compat because the result is a record) and the code for getting properties does not know about the calling conventions  of their setter and getter (because that is not part of their RTTI but only the code pointer to them) and thus always assumes standard calling convention.

Edited by Stefan Glienke

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×