VSoft Technologies Blogs


VSoft Technologies Blogs - posts about our products and software development.

FinalBuilder's IDE is message driven. When you select an action in an actionlist for example, the actionlist view publishes a message to which other parts of the IDE can subscribe. The publish/subscribe mechanism used in the IDE has gone through a few revisions over the years, in the quest for the perfect publish/subscribe mechanism...

Of course when I say "perfect", what I'm really after is the architecture that is the easiest to use, and more importantly, the easiest to maintain. In earlier versions of the IDE, we used simple interfaces with an event args object that can carry various payloads :

IEventArgs = interface
  function EventID : integer;
  property IntfData : IInterface..
  property StringData : string...
  property IntData : integer..
ISubscriber = interface
  procedure OnIDEEvent(const AEventArgs : IEventArgs);

This has worked well for a few revisions, but with FinalBuilder 7 we have a new IDE, the number of messages being published has doubled, and code was starting to look pretty ugly. Each subcriber's OnIDEEvent method was one long case statement... and we were always having to refer back to where the message was sent from to confirm exactly what the payload was. Not at all scalable. So I started looking for new ideas. In a c# app that a colleague here wrote, he used interfaces with generics to create a publish/subscribe mechanism :

public interface IConsumer { }
public interface IConsumerOf : IConsumer where T : IMessage
  void Consume(T message);

Well, Delphi 2010 has generics support and I'm already making extensive use of them (hard to imagine now how I did without them!), so I figured I'd see if I could use the same technique in delphi :

IMessage = interface

ISubScriber = interface

//note the constraint so only message objects can be published
ISubScriberOf = interface(IConsumer)
//No guid for generic interfaces!
  procedure Consume(const message : T);

TMyMessage = class(TIntefacedObject, IMessage)
TMyOtherMessage = class(TIntefacedObject, IMessage)
TMySubscriber = class(TIntefacedObject,ISubScriberOf, ISubScriberOf)
  procedure Consume(const message : TMyMessage);overload;
  procedure Consume(const message : TMyOtherMessage);overload;

Works perfectly.... except now I have a bunch of methods named Consume... and my class needs to implement an interface per message. According to my well thumbed copy of Delphi in a Nutshell, a class can implement up to 9999 interfaces so that's not a problem, but, it's just not quite as neat as I'd hoped. It worked well in the c# app as there were only a few message types. Most of my subscribers are handling 20+ messages, navigating 20+ Consume methods isn't all that maintainable.

Back to looking at the mother of all case statements in my existing architecture, it reminded me of a WndProc method and that got me thinking. How do message handlers work in the VCL? You know, this sort of thing :

procedure WMActivate(var Message: TWMActivate); message WM_ACTIVATE;

I had always just assumed it was compiler magic... (back to Delphi in a Nutshell again), well they are dynamic methods, which are invoked via TObject.Dispatch. This method takes a message record, and based on the message id, finds the matching method in the object's dynamic method table (which is compiler generated), if not found then it looks up the class heirachy and then eventually calls DefaultHandler if no match was found. Delphi's windows controls use this mechansim to dispatch windows messages, but there's really nothing windows specific about it and it looks like it could be used for any messages. The key is that the messages must be records because TObject.Dispatch treats them as such, and it looks at the first 4 bytes (DWoRD) as the message id. If you look in Messages.pas you will see that most messages are typically 16 bytes long, and they are packed records. In my initial tests everything worked fine just keeping the first 4 bytes for the message id, however in my IDE strange things happened (random av's). It turns out the messages really do need to be at least 16 bytes. So my message types look like this :

  TMyMessage = packed record
    MsgID : Cardinal;
    Unused : array[1..12] of Byte;
    MyPayload : whatever;
    constructor Create(APayload : whatever);

Note that I use a constructor on the record. Constructors on records are really just psuedo constructors.. but they serve a purpose here.. this is where I make sure the message gets assigned the correct message id (from a constant). Without the constructor we would have to assign it to the message before it is sent.. that opens the possibility of used the wrong id by mistake, which could cause random access violations :

constructor TMyMessage.Create(APayload : whatever);
  MsgID := IDE_MYMESSAGE; //constant 
  MyPayLoad := APayload;

One caveat with constructors on records is that they must have at least one parameter, so if your message has no payload then just use a dummy parameter. Our Subscriber interface now looks like this :

  ISubscriber = interface
    procedure Dispatch(var Message);

The Dispatch message on the interface is declared the same as TObject.Dispatch, and TObject.Dispatch is our actual implementation on our subscriber class so we don't need to do much other than declare that it implements the interface, and then provide the message handlers :

TMySubscriber = class(TInterfacedObject,ISubscriber,...)
  procedure DoMyMessage(var Message : TMyMessage);message IDE_MYMESSAGE;

Our publisher interface is quite simple too :

IPublisher = inteface
  procedure SendMessage(var Message);
  procedure Subscribe(const subscriber : ISubscriber; const filter : integer = 0);
  procedure UnSubscribe(const subscriber : ISubscriber);

The SendMessage method takes an untype var parameter (just like Dispatch), so any message type can be passed to it. The Subscribe method has an extra parameter, filter, which allows you to specify which messages a subscriber is interested in. If it's not specified then all messages will be passed to the subscribers Dispatch method. The filter isn't strictly neccessary, but it does provide an opportunity for a small optimisation if the subscriber really only handles a few messages.

So is this the "perfect" publish/subscribe mechanism for delphi? Probably not. It's kinda neat how it's using something that's been there in TObject probably back as far as Delphi 1 (I will have to dig out my D1 source disk and have a look!). We spent a day replacing our old mechanism with this one throughout the entire FB7 IDE, and it's performing flawlessly. I've read that dynamic methods are a bit slower than non dynamic methods.. but to be honest we haven't noticed any change in performance.. what we have noticed is how much easier our code is to read and maintain.

A sample D2010 app with full source is available here.

Hybrid Version Control

Distributed verison control systems are gaining in usage and popularity, but many organisations still use traditional centralised VCSs like Subversion and Visual Source Safe. Recently I've been using a hybrid setup and getting many of the benefits of a DVCS without needing to move the whole team to a new VCS platform.
When I started with VSoft a few months back my first chunk of work was to create Mercurial actions for the upcoming FinalBuilder 7. It was the first time I'd used a DVCS and after the initial shock I became quite fond of it.
Internally we use a CVCS - Surround SCM. If you've never used Surround you can think of it as VSS done right. It uses similar concepts and abstractions but without all the pain and frustration. We're considering moving to a DVCS but haven't yet worked out if the increased flexibility is worth the extra overhead for our relatively small dev team.
After getting to know Mercurial though, I knew what I was missing out on. Primarily for me that was version control of my local changes. This is becoming more important as FinalBulder 7 gets closer to completion and breaking the build becomes a bigger deal. I also like being able to easily clone and sync my repositories locally as a basic backup strategy.
The setup I've come up with couldn't be simpler. If I'm going to work on a VS.NET solution I will:
  • check out the solution from Surround
  • hg init the solution directory to create a repository
  • hg commit -A which adds/removes all file changes to the repository
  • work locally, committing whenever I feel like it
  • when the solution is in a state where it won't break the build, check it in to Surround
To make things easier I've set up this alias in my Mercurial.ini:
cam = commit -A -m
So to commit I just hg cam "commit message".
Recently I started checking my .hg directory into Surround as well. That allows me to maintain the history of all my local commits, without cluttering up the Surround check-in logs.


There's some things to be aware of when using a hyrbid system like this. While you could still use Mercurial for merging work from different developers it is much more complex than in a pure DVCS setup. I'll leave it to you to work out the details: it's not something I plan on ever doing. 
If you're checking your .hg folder in to your CVCS you need to be careful that it doesn't become corrupted through concurrent updates/merging. Of course if it does become corrupted you don't lose much by deleting and recreating it, because the check-in history for your major changes is in the CVCS's check-in logs.

Other uses for Mercurial

Because Mercurial repositories are so easy to create I've started using them for all sorts of things. For example, at home I have a perl script that runs nightly and exports the contents of my Wordpress blogs to XML. Previously I included the date in the filename and ended up with (literally) hundreds of files in my backup directory. 
Now I've set up a Mercurial repository, removed the date component from the filename and have my backup script commit after it downloads the latest version. The directory is now a lot cleaner as well as being smaller, because Mercurial only stores the changes between each night's backup.

Further reading

While running a hybrid system doesn't give you all the advantages of a pure DVCS it is a major improvement over a plain CVCS. It also allows you and your team to get comfortable with the DVCS methodology before moving away from CVCS completely.
For more information on Mercurial, see:


Using interfaces and reference counting in Delphi works great for the most part. Its a feature I use a lot, I'm a big fan of using interfaces to tightly control what parts of a class a consumer has access to. But, there is one big achillies heel with reference counting in Delphi, you cannot keep circular references, at least not easily, without causing memory leaks.

Consider this trivial example :

IChild = interface;
IParent = interface
  procedure AddChild(const AChild : IChild);

IChild = interface

TParent = class( TInterfacedObject, IParent )
  FChild : IChild;
  procedure AddChild(const AChild : IChild);
  destructor Destroy; override;

TChild = class( TInterfacedObject, IChild )
  FParent : IParent;
  constructor Create( AParent : IParent );
  destructor Destroy; override;

constructor TChild.Create(AParent: IParent);
  inherited Create;
  FParent := AParent;

destructor TChild.Destroy;
  FParent := nil;
procedure TParent.AddChild(const AChild: IChild);
  FChild := AChild;

destructor TParent.Destroy;
  if Assigned( FChild ) then
    FChild := nil;

procedure Test;
  MyParent : IParent;
  MyChild : IChild;
  MyParent := TParent.Create;
  MyChild := TChild.Create(MyParent);
  MyChild := nil;
  MyParent := nil;

Both parent and child are now orphaned and we have no reference to them and no way to free them! Ideally, the parent would control the life of the child, but the child would not control the life parent.

So how can we get around this? Well a technique that I have used a lot in the past is to not hold a reference to the parent in the child, but rather just a pointer to the parent.

TChild = class(TInterfacedObject,IChild) 
  FParent : Pointer; 

constructor TChild.Create(AParent : IParent); 
	FParent := Pointer(AParent); 
function TChild.GetParent : IParent; 
  result := IParent(FParent); // if the parent has been released the we are passing out a bad reference! 
                              // a nil reference would be preferable as it's easy to check. 

This works well for the most part, but it does have the potential for access voilations if you do not understand or at least know how the child is referencing the parent.

For example :

var child : IChild; 
parent : IParent 
  parent := TParent.Create; 
  child := TChild.Create(parent): 
  parent := nil; //parent will now be freed, since nothing has a reference to it.
  parent := child.GetParent; //kaboom 

One of my collegues kindly pointed out that C# doesn't suffer from this problem and he uses circular references all the time without even thinking about it. While discussing this, he mentioned the WeakReference class in .NET. It basically allows you to hold a reference to a object without affecting it's lifecycle (ie, not influencing when it will be garbage collected). I figured there must be a way to do this in Delphi, and so set about creating a WeakReference class for Delphi.

I wasn't able to find a reliable way to do this with any old TInterfacedObject descendant, however by creating a TWeakReferencedObject class and the use of generics on Delphi 2010 I did manage to implement something that works well and is not too cumbersome. Lets take a look at our Child/Parent example using a weak reference.

The important part in this is the use of the WeakReference to the parent in the Child class. So instead of declaring

FParent : IParent;

we have

FParent : IWeakReference<IParent>;

We create it using

FParent := TWeakReference<IParent>.Create(parent); //value is an IParent instance

This is how our TChild.GetParent /SetParent methods look now :

function TChild.GetParent: IParent; 
  if FParent <> nil then 
    result := FParent.Data as IParent 
    result := nil; 

procedure TChild.SetParent(const value: IParent); 
  if (FParent <> nil) and FParent.IsAlive then 
  FParent := nil; 
  if value <> nil then 
    FParent := TWeakReference<IParent>.Create(value); 

Note the use of the IsAlive property on our weak reference, this tells us whether the referenced object is still available, and provides a safe way to get a concrete reference to the parent.

I still think this is something that could be solved in a better way by the delphi compiler/vcl guys n girls.

Hopefully someone will find this useful, the code is available for download here - Updated Sunday 28/3/2010

Feedback welcolme, I'm about to start making extensive use of this code, so if you see any holes then please do let me know!

No more flash ads, ever.

Yesterday, I stopped all of our flash based ad campaigns... forever.

In late 2008, we started a large campaign which introduced our first flash ads. We had some fun making these and received mixed responses - some outrage, some love. But overall, I feel that just by using flash ads to communicate, we were becoming a little too closely affiliated with the many scam like flash ads. You know the ones, those before-and-after-weight-loss-body-shots.

Advertising was once a powerful method of communicating, but no more. Thanks to the lowest common denominators in advertising, everyone suffers. Ad blockers are the proof. When the web is filled with before-and-after-weight-loss-body-shots, we block every ad. The 1% of useful information from advertising is not worth the 99% of junk!

So what to do? How can we communicate with those who might want what we've got, without the medium implying we're offering cruft?

Two ways: education and community involvement.

From now on, these two methods will form the pillars of our overall marketing. A reply to a question on stackoverflow. A response to a customer on twitter. It's how FinalBuilder got it's start, before we could afford ad space, and now we're coming back to it because it's authentic and meaningful.

Oh, and we have a few other, more exciting plans to get involved in the community. Watch this space.

FinalBuilder Roadmap for 2010

We don't usually have a published roadmap. I generally don't like to talk too much about the next version until I have something to show (ie when we are close to beta), however it's been a while since we release FB6 and some customers have asked what our plans are.

I will say that FinalBuilder and FinalBuilder Server are being actively worked on. Below is just a general outline of what we are up to :

FinalBuilder 7.0:

1) Full Unicode support. This is something that we have been working on for the last year. It meant finding replacements for many libraries and components we use, the biggest of which was the Active Scripting support. We ended up having to write our own active scripting library as the old one was impossible to move forward with (the vendor has long since disappeared). This work is mostly completed.

2) A new IDE - with the ability to open multiple projects, and hopefully be able to step into included projects. This required some major re-architecting of the core and the IDE and that is something we are still working on.

3) General enhancements to existing actions and some new actions. As usual, we have a long todo list to keep us busy.

FinalBuilder Server 7.0

FinalBuilder Server 7.0 will be a revision of the FBServer 6.0 code base. (Note that this has changed from my forum post in Nov. due to resourcing issues).

1) Support for more version control triggers. MKS, Clearcase and possibly Git have been identified as popular targets.

2) Enhancements to the user interface.. cleaning up html, more use of client side script to reduce postbacks.

3) Performance enhancements where possible.

We will also be looking at feature requests to see what else we can implement, but many will be implemented in a future product (see below).

FinalBuilder Server vNext + 1

Along side the developement of FinalBuilder, we have had another project running over the last year that was intended to be FinalBuilder Server 7. Due to a lack of resources (or more to the point, a lack of success in recruiting) we had to rethink our plans for this product. It's a large project, and it became clear over the last few weeks that it was not going to be ready to release alongside FinalBuilder 7. This project was born out of the feature requests and feedback from users, and an in depth look at what people were really asking for and why, as well as our own future needs (we are our own best customer!). It was clear quite early on that the architecture of FinalBuilder Server was not capable of supporting some of the features, and neither was ASP.NET.

So we started fresh. FB Server vNext (actual product name yet to be decided) was designed from outset to support work flow, build agents (possibly also on other platforms), tight integration with bug tracking systems and version control. We are also focusing more on the release management side of things.. management of build artifacts, release notes, tracking which issues are related to which builds, which checkins are related to which builds etc.


Now what you are probably wanting to hear is when will the next versions be released. I'm not going to make any promises here, but we are aiming for FinalBuilder 7 and FBServer 7 at the end of Q1 next year. FinalBuilder Server vNext will come later in the year.

The biggest blocking issue for us at the moment is a lack of manpower (or woman power!). We are finding it hard to recruit suitable people. Canberra is a difficult place to run a software business, we have to compete with the many (both federal and ACT) government departments for people... and despite the election promises the government has only gotten larger over the last 2 years. We will continue our recruitment drive in the new year (but feel free to submit your resume - if you have the right to work and live in Australia).