dmitrybv 3 Posted Monday at 11:08 PM 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
Remy Lebeau 1596 Posted yesterday at 02:57 AM 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
Lajos Juhász 319 Posted yesterday at 06:44 AM (edited) 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 yesterday at 06:46 AM by Lajos Juhász Share this post Link to post
dmitrybv 3 Posted 11 hours ago 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
Stefan Glienke 2141 Posted 6 hours ago (edited) 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 6 hours ago by Stefan Glienke Share this post Link to post