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

Automatic LeaveMethod call



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:

IAutoLeaveCaller = interface

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

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

destructor TAutoLeaveCaller.Destroy;
FSession.LeaveMethod(_Level, _MethodName);

Which is used like this:

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


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.



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.


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:



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?


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.


Okay, thanks for the update.