
Back to Blog index.
Sometimes, Delphi developers get confused about the order in which their units get initialized and finalized. For instance, if you have code in an initialization section of a unit that depends on the initialization of another unit, chances are this coupling can break if you are not aware of the rules in which units are initialized and finalized.
If you take a look at the documentation that comes with Borland Delphi (or Borland Developer Studio), it just simply says that the order depends on the order which the units are listed in the project (.DPR) file. For simple cases, this is perfectly true, but it can get more difficult than that. Lets see a simple example with four units.
First, assume you have project file that looks like this:
program Project1;
uses
Unit1 in 'Unit1.pas',
Unit2 in 'Unit2.pas',
Unit3 in 'Unit3.pas',
Unit4 in 'Unit4.pas';
begin
{ some code here }
end.
Now, lets say all the units 1-4 look similar to this:
unit Unit1;
interface
implementation
uses Windows;
initialization
OutputDebugString('Unit1 Initialization');
finalization
OutputDebugString('Unit1 Finalization');
end.
In this simple situation, the units are initialized in order 1 to 4, and finalized in the opposite order, 4 to 1. This is easily proved by looking Delphi's Event Log debug window output:
Debug Output: Unit1 Initialization Process Project1.exe (1960) Debug Output: Unit2 Initialization Process Project1.exe (1960) Debug Output: Unit3 Initialization Process Project1.exe (1960) Debug Output: Unit4 Initialization Process Project1.exe (1960) ... Debug Output: Unit4 Finalization Process Project1.exe (1960) Debug Output: Unit3 Finalization Process Project1.exe (1960) Debug Output: Unit2 Finalization Process Project1.exe (1960) Debug Output: Unit1 Finalization Process Project1.exe (1960)
Now, if you would change the order of the units in the Uses clause of the .DPR from "1-2-3-4" to say, "3-1-2-4", then the initialization order would be the same as the order in the Uses clause: 3-1-2-4. And, as you have learned, the finalization would happen in reverse order, giving 4-2-1-3. Easy, no?
Now, lets return to the basic order again (order 1-2-3-4). Since units often use other units, it is important to learn that this also affects the unit initialization order. So, what would happen if you would change Unit1 to use Unit4 in the interface section? Like this:
unit Unit1;
interface
uses Unit4; <-- NOTE THIS
implementation
uses Windows;
initialization
OutputDebugString('Unit1 Initialization');
finalization
OutputDebugString('Unit1 Finalization');
end.
Now, what would the order look like in this case? Think of a chain. The initialization starts from the first unit in the .DPR Uses clause, so it goes to Unit1. But, if Unit1 uses any other unit (in this case, Unit4), that unit is initialized first. This is important to remember. So in this case, the output would be:
Debug Output: Unit4 Initialization Process Project1.exe (2680) Debug Output: Unit1 Initialization Process Project1.exe (2680) Debug Output: Unit2 Initialization Process Project1.exe (2680) Debug Output: Unit3 Initialization Process Project1.exe (2680) ... Debug Output: Unit3 Finalization Process Project1.exe (2680) Debug Output: Unit2 Finalization Process Project1.exe (2680) Debug Output: Unit1 Finalization Process Project1.exe (2680) Debug Output: Unit4 Finalization Process Project1.exe (2680)
So suddenly, the order is 4-1-2-3! I hope this clarifies the situation.
This appears to be one-month old news, but I heard just lately that Microsoft is planning to introduce products that are geared towards musicians. Since most "The" music products are on Mac, it would be nice to have some real broadliner like Microsoft to have their own products as well.
Enter code-name Monaco. This could become the Windows version of Apple's GarageBand application. Do we get Monaco on MSDN once it is ready? :-) I'd be one to download it immediately.
Regulatory compliance, accounting, law, rules by the book, ... Ahem, not very interesting for us developers. But, it ought to be especially if you are developing applications for large corporations and the public sector.
There's a nice article on MSDN that demystifies a lot of questions up in the air when it comes to Sarbanes-Oxley (SOX), HIPAA, and others.
There's a very informative, short little article in the May-June issue of the TechNet Magazine about Windows Vista security features.
The article talks about User Account Control (UAC), data encryption and service hardening. Highly recommended reading for every Windows developer, no matter if you use C# or Delphi!
If you are using Visual Studio 2005 for developing .NET applications, there's a quite little-known debuggingn feature in this new release called Managed Debugging Assistants (MDAs). These little, umm, we could call them helpers of sort, analyze your application and the environemnt in which it is running (CLR to be more specific), and let you know if there's something odd happening, such as your stack becoming corrupted.
The newest MSDN Magazine issue has a great little article that investigates this feature. If you want to learn something new every day, this is your chance for today.
I recently ran into this month-old article, which teaches data access using Visual Studio 2005 and .NET 2.0. This is a beginner’s article, which just gets you going, but oftentimes that's all a beginner would need.
The nifty XMLHttpRequest object, which is the basics of AJAX (Asynchronous JavaScript and XML) has been implemented kind of the same in the most important web browsers, but so far there hasn’t been any official standards. As AJAX is enjoying its current media shine, it is time for a standardized version of the XMLHttpRequest object. Luckily, W3C has already started this process for the benefit of all developers (and eventually, users), and the results can be seen on their web pages. The current status is Working Draft.
In the past few years, many key R&D team members have left Borland, which in my opinion has been a blow to the company, despite what the official communications say. For example, we all know that Anders Hejlsberg left for Microsoft to build .NET and C#, but also Danny Thorpe, the compiler architect for many years, has left to name a few.
The latest information came through Falafel Software's press releases, where one can read that Ramesh Theivendran (the web/database guy) has also left Borland. Well, nobody is indispensable in business, but I’m starting to loose my faith if these news continue.
Of course, not everybody is leaving. According to Allan Bauer's blog, they now have a new compiler guy in the team, and also have hired an old-timer, ex-Borlander Steve Shaughnessy. He worked on the Paradox DOS team, remember the product? That said, only time will tell what will become of the "DevCo". I’m waiting for the news with interest. Luckily, I’m not betting all on one card, as I have learned to love Visual Studio as well as Delphi.
The eWeek magazine quotes a recent Gartner research about Windows Vista "ready" PCs, and concludes that about half of current PCs meet Vista's hardware requirements. Personally, I think two technical details matter most here: the amount of RAM in the PC and the video/graphics card the PC happens to have. Of course, the final system requirements are yet to be shaped, but I would bet these are the two main things.
However it should be noted that Vista will also run without the glass-like Aero graphics engine. For us software developers, it is important to learn that when your application run on Vista, you actually know that the PC the user has is powerful in today's standards. But that doesn’t mean you can get sloppy with performance and tuning of your code.
Microsoft is without doubt wanting to get all abroad the Visual Studio ship, and thus they have also compiled a informational page for Borland Delphi customers. Since some Delphi users have switched to Visual Studio (especially version 2005), this kind of information has its uses.
Originally, the page had a question about Delphi's support for Java, but obviously that was in error. This problem has now been fixed.
The April 2006 issue of Tietokone popped into my mailbox today. From this issue, you can find three of my articles. I'll update my pages to reflect the latest info soon.
Enjoy!
The spring is few weeks late here in Helsinki, but I decided today to take my bike away from the garage and ride it to maintenance. It didn't take long after it was done, and so it was time for the first ride of the spring.
Can't say it was a much as fun as in summer, but after a six month absence, it feels great to be able to feel the freedom again. Though I like my BMW, I prefer my bike for the additional fun it provides.
I today received an e-mail from Microsoft I had hoped I would. I got an confirmation that I've been selected to receive the MVP Award for 2006 as well.
Thank you Microsoft! I'm vrey pleased to be on board again, since it has been a great year. Also I'm looking forward to interacting with the Visual Studio product group as they prepare for the Orcas release. Stay tuned for more info.
Despite what some of my colleagues and friends tell me, I don't need to use Firefox for my daily surfing, neither at home or at the office. I'm still using IE, as it works the best for me.
Now, the only reasons I use Firefox are Flash-only web sites and some downloads. Flash is common in many "better" web sites, but I never liked those animations, and thus have disabled the Shockwave plug-in in my IE. Instead, I switch to Firefox when I need to see Flash content.
But today I wanted to see how the Download Manager in Firefox could cope with large downloads. I was downloading a DVD ISO image from an Finnish FTP site, when the screen started looking like this:

As you see, Firefox cannot count properly. Okay, it is the old 1.0.7 version, but I cannot help thinking about the assertion some people make that open-source leads to better code. Using 32 bit integers for file size calculatations in the 2000s. Hello...!
Sometimes, malicious software can hit your PC even if you are running the latest antivirus products and a proper firewall. In those situations, it is good to have proper tools to get rid of those little germs.
You might have heard that Microsoft has developed a tool called Malicious Software Removal Tool (MSRT or MRT), which is updated once a month using Windows Update or Automatic Updates. But what if you wanted to run the tool just now?
If you are following the instructions given by Microsoft, you should browse to Microsoft's web site, and run the tool from there. Okay, this is good and guarantees the latest version, but what if you want to eliminate the Internet connection from the infected PC and cannot surf the net? In those situations, it is helpful to know how to run the tool in a standalone mode.
Here's how: if you have updated your PC automatically (as you should have), then the Malicious Software Removal Tool is already on your PC. It lives in the Windows System32 directory under the name MRT.exe. Simply run this executable (or type "mrt" on the command line) and there you go. Easy once you know how!

You might have noticed that there exists an C# FAQ (Frequently Asked Questions) on the MSDN blogs web site.
I decided to help with the FAQ, and wrote two simple entries to the FAQ. Scott Nonnenberg, the Program Manager for the Visual C# Team helped me to publish the entries. More entries are on the works, and in the meantime, feel free to comment the entries on the site.
› Blog Archive