Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: Use of Attributes & RTTI causing unexplained AV



Permlink Replies: 5 - Last Post: Jan 31, 2015 12:50 PM Last Post By: Robert Love
Adam Brett

Posts: 9
Registered: 11/18/04
Use of Attributes & RTTI causing unexplained AV
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 29, 2015 1:48 AM
Not sure which group to post this on ... but a search for RTTI showed more questions on this group than any other, so I guess here is OK :)

I have an application which uses RTTI.

I have this declaration at the top of the project:

{$RTTI EXPLICIT METHODS ([vcPublished])}
{$METHODINFO ON}

And then (up to now) I have just been using RTTI to enable Forms to find methods on Business Objects, and use these methods in their GUI:

procedure TabActionPanel.LoadPublishedMethods(aBizObj: TabBizObj);
var
rttiCont: TRttiContext;
rttiMethod : TRttiMethod;
rttiAttri: TCustomAttribute;
aImageIndex: Integer;
aCaption: String;
begin
rttiCont:= TRttiContext.Create;
rttiType:= rttiCont.GetType(aBizObj.ClassType);
try
for rttiMethod in rttiType.GetMethods do
if POS('_', rttiMethod.Name)>0 then
begin
aCaption:= StringReplace(rttiMethod.Name, '_', ' ', [rfReplaceAll]);
AddButton(aCaption, OnRttiClick);
end;
except
end;
end;

FIRST ISSUE:

From time to time my GUI does not show all the Methods on the BizObject. It usually shows them, and "switching it off and on again" usually fixes things ... but I only get about 97% consistency with this code (97% is a guess by the way). Users running older versions of Windows seem particularly prone to the code failing to find all the methods on the object.

I was kind of living with the above problem, and hoping when I upgrade from XE2 to XE7 (or 8) basic improvements from EMB might fix the problem.

If there is something I am doing wrong in the above code to cause this occasional "RTTI-fail" I would be grateful to hear about it.

However now I have a bigger problem:

I am trying to move to the following code, which adds use of an Attribute on the BizObj method, to allow the tool-button on the form to include an icon / image.

ImageIndexAttribute = class(TCustomAttribute)
public
Value: Integer;
constructor Create(aImageIndex: Integer);
end;

BizObj Method would then be:

published
[ImageIndex(6)]
procedure New_Email;

procedure TabActionPanel.LoadPublishedMethods(aBizObj: TabBizObj);
var
rttiCont: TRttiContext;
rttiMethod : TRttiMethod;
rttiAttri: TCustomAttribute;
aImageIndex: Integer;
begin
rttiCont:= TRttiContext.Create;
rttiType:= rttiCont.GetType(aBizObj.ClassType);
try
for rttiMethod in rttiType.GetMethods do
if POS('_', rttiMethod.Name)>0 then
begin
aCaption:= StringReplace(rttiMethod.Name, '_', ' ', [rfReplaceAll]);
for rttiAttri in rttiMethod.GetAttributes do
if rttiAttri is ImageIndexAttribute then
aImageIndex:= ImageIndexAttribute(rttiAttri).Value;
Toolbar.AddButton(aCaption, aImageIndex, OnRttiClick);
end;
except
end;
end;

So: I have added a for-loop to iterate the method for Attributes and am trying to capture the ImageIndex.

All the on-line help says this should work.

However I am getting a simple AV on the line

for rttiAttri in rttiMethod.GetAttributes do

Bizarrely, when I was testing the code 2 months ago this line compiled and worked OK. Now it does not work at all.

Any ideas about things I may have changed which might cause this?
Robert Love

Posts: 155
Registered: 5/3/07
Re: Use of Attributes & RTTI causing unexplained AV
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 29, 2015 5:55 AM   in response to: Adam Brett in response to: Adam Brett
2 problems stand out in the code, there code be more.

Adam Brett wrote:
rttiType:= rttiCont.GetType(aBizObj.ClassType);

A/V could occur here if aBizObj is nil

except
end;

You are hiding exceptions, this is very bad idea, if you are expecting something specific trap for it, otherwise unexpected exceptions are ignored causing problems on later code that expected this to execute without error. Just incase.... checking for an EAccessViolation is a bad idea, fixing the code that causing the EAccessViolation is what you need to do.

Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: Use of Attributes & RTTI causing unexplained AV
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 29, 2015 11:34 AM   in response to: Robert Love in response to: Robert Love
Robert wrote:

rttiType:= rttiCont.GetType(aBizObj.ClassType);
A/V could occur here if aBizObj is nil

Also make sure that GetType() is not returning nil, either, otherwise an
AV will occur when rttiType.GetMethods() is called.

You are hiding exceptions, this is very bad idea

Agreed.

--
Remy Lebeau (TeamB)
Adam Brett

Posts: 9
Registered: 11/18/04
Re: Use of Attributes & RTTI causing unexplained AV
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 29, 2015 3:09 PM   in response to: Remy Lebeau (Te... in response to: Remy Lebeau (Te...
rttiType:= rttiCont.GetType(aBizObj.ClassType);
A/V could occur here if aBizObj is nil

It is never nil in all my inspections of it.

Also make sure that GetType() is not returning nil, either, otherwise an
AV will occur when rttiType.GetMethods() is called.

This is never nil either.

The error always occurs in this line:

for rttiAttri in rttiMethod.GetAttributes do

When I hover over "GetAttributes" I get a clear indication of an AV.

I get through this line:

if POS('_', rttiMethod.Name)>0 then

and hovering over "Name" shows the currently-accessed method's name ... so that part is all working fine, or at least something is being assigned to rttiMethod.

But that something doesn't seem to include Attribute-related data.

Is there a need to activate the ability to access Attributes with some extra bit of code I have left out?


You are hiding exceptions, this is very bad idea

Agreed.

You are dead right, but the code has to work for now so the exception is in place to stop everyone in the office's computers from beeping with exceptions every second.

Sorry to add confusion by including this empty except.
Robert Love

Posts: 155
Registered: 5/3/07
Re: Use of Attributes & RTTI causing unexplained AV
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 31, 2015 12:50 PM   in response to: Adam Brett in response to: Adam Brett
Adam Brett wrote:
rttiType:= rttiCont.GetType(aBizObj.ClassType);
A/V could occur here if aBizObj is nil

It is never nil in all my inspections of it.

Write your code to error if it is NIL, don't make an assumption, then you get error messages that make sense.

The error always occurs in this line:
for rttiAttri in rttiMethod.GetAttributes do
When I hover over "GetAttributes" I get a clear indication of an AV.

This to me indicates that RttiMethod may be nil, if it's not maybe you can determine which method it is crashing on.

Honestly, lots of data is missing about this problem. First we don't have the code for the class that the RTTI is looking at. See if you can reduce this into a simple test project.

It's also possible that you have a memory overwrite occuring earlier in the program that is corrupting the RTTI data.
Markus Humm

Posts: 5,113
Registered: 11/9/03
Re: Use of Attributes & RTTI causing unexplained AV
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 29, 2015 10:45 AM   in response to: Adam Brett in response to: Adam Brett
Hello,

while I hope Robert's answers are of help to you already (and he's right
in both cases), the RTL groupd would have been a good candidate for the
post.

Greetings

Markus
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02