Join 34,000+ subscribers and receive articles from our blog about software quality, testing, QA and security.
 

Automatic LeaveMethod call


#1

Hi,

it just occurred to me that EnterMethod/LeaveMethod could use a feature (OK, not a feature but rather a known but undocumented behaviour) of the Delphi compiler for automatically doing a LeaveMethod call:

[code]type
IAutoLeaveCaller = interface
end;

type
TAutoLeaveCaller = class(TInterfacedObject, IAutoLeaveCaller)
private
FSession: TSiSession;
FLevel: TSiLevel;
FMethod: WideString;
public
constructor Create(_Session: TSiSession; _Level: TSiLevel; const _MethodName: WideString);
destructor Destroy; override;
end;

constructor TAutoLeaveCaller.Create(_Session: TSiSession; _Level: TSiLevel; const _MethodName: WideString);
begin
inherited Create;
FSession := _Session;
FLevel := _Level;
FMethod := _MethodName;
FSession.EnterMethod(_Level, _MethodName);
end;

destructor TAutoLeaveCaller.Destroy;
begin
FSession.LeaveMethod(_Level, _MethodName);
inherited;
end;
[/code]

Which is used like this:

[code]procedure TSomething.DoWhatever;
begin
TAutoLeaveCaller.Create(FSession, lvVerbose, ‘TSomething.DoWhatever’);


end;
[/code]

What this does is creating an implicit interface variable which will be kept until exiting the method, in which case it will automatically be freed.

This would solve the problem of having to insert LeaveMethod wherever the mehod exits (eg. calling exit or raising an exception). Of course, that’s even worse than adding try…finally in terms of performance, but very convenient to code.

Of course by wrapping it into a TSiSession-Method it could be made even easier to use.

twm


#2

Hello twm,

Good idea, thanks. It’s actually so good that this feature already exists in SmartInspect ;). The session method is called TrackMethod and does exactly what you are suggesting. And as you have already described, it uses the interface/reference counting feature to implement this.


#3

Hi Tobias,

Hm, I just checked the PDF manual and there is only one place where TrackMethod is mentioned: Chapter 2.2 What’s New / SmartInspect 1.3.1

It’s in the online help, though and even linked from EnterMethod, so I probably would have found it if I hadn’t thought myself so very clever… :frowning:

twm


#4

Although the most important features are also available in the PDF manual, many of the library-related features are only in the API documentation. We will look into it if and how we can integrate the API documentation directly into Delphi, so that it’s easier to lookup features/methods.

By the way, did you have the time to look at the asynchronous feature of the SmartInspect 3.0 EAP releases?


#5

unfortunately not. I changed the app for which I originally wanted this to work differently and, as always I am currently too busy to do any meaningful testing.


#6

Okay, thanks for the update.