Feeds

Binary interfaces in component development

A template class with static data - how can that possibly go wrong?

Intelligent flash storage arrays

What’s the solution? Unfortunately, the solution is to choose a software architecture that doesn’t expose you to this incoherence between dynamic libraries and static data. In the example we’ve seen, the problem comes from how dynamic libraries work on windows, but on Linux, there are equivalent ways to get stung by this issue. [If people are interested leave a comment and we can look at it next time.]

In conclusion, what do we think the specific problems are with this architecture, which then led us into this trap? Firstly, the person who came up with the idea of using a class template to implement the singleton pattern needs to be spoken to severely. Management of static data is always so problematic in C++ that when it is used, the location of its definition must be carefully considered, so that the same definition is used in all places that it is needed. To choose an implementation technique such as templates, that obliges the compiler to put definitions of the same piece of static data in every object file for a pattern that exists to manage static data, is a little thoughtless.

Secondly, there are times when we do really need static data in templates. Not for a Singleton pattern, we’re probably all agreed; but sometimes it can be an extremely powerful implementation tool. In these cases, we should manage the location of the definition carefully, by not exposing the template definitions in the interfaces and instead using explicit instantiation at the appropriate place. The cost of explicit instantiation is that the template can only be used for types that are known to the template definition in advance; but if you don’t do this, you guarantee later problems for yourself, when running with dynamic libraries on Windows.

Finally, you can use the same code with explicit instantiation – it now works but this shouldn’t be regarded as a fix for the implementation, as the cost of having to know all Singleton types in advance is a big problem for this class. And a further note: for all the trouble caused by the template implementation, could we not just have written a non-template version, and duplicated the two lines of code whenever we needed to?

Resources:

The Win32 Portable Executable File Format: http://msdn.microsoft.com/msdnmag/issues/02/02/PE/.

The PEBrowse Windows disassembler and static-analysis tool: http://www.smidgeonsoft.prohosting.com/pebrowse-pro-file-viewer.html.

The program linker and what it does: http://www.microsoft.com/msj/0797/hood0797.aspx.

The Sourcecode used for this article, in a zip archive.

Secure remote control for conventional and virtual desktops

More from The Register

next story
Download alert: Nearly ALL top 100 Android, iOS paid apps hacked
Attack of the Clones? Yeah, but much, much scarier – report
NSA SOURCE CODE LEAK: Information slurp tools to appear online
Now you can run your own intelligence agency
Microsoft: Your Linux Docker containers are now OURS to command
New tool lets admins wrangle Linux apps from Windows
Microsoft adds video offering to Office 365. Oh NOES, you'll need Adobe Flash
Lovely presentations... but not on your Flash-hating mobe
You stupid BRICK! PCs running Avast AV can't handle Windows fixes
Fix issued, fingers pointed, forums in flames
HTML5 vs native: Harry Coder and the mudblood mobile app princes
Developers just want their ideas to generate money
prev story

Whitepapers

Designing and building an open ITOA architecture
Learn about a new IT data taxonomy defined by the four data sources of IT visibility: wire, machine, agent, and synthetic data sets.
10 threats to successful enterprise endpoint backup
10 threats to a successful backup including issues with BYOD, slow backups and ineffective security.
Forging a new future with identity relationship management
Learn about ForgeRock's next generation IRM platform and how it is designed to empower CEOS's and enterprises to engage with consumers.
Reg Reader Research: SaaS based Email and Office Productivity Tools
Read this Reg reader report which provides advice and guidance for SMBs towards the use of SaaS based email and Office productivity tools.
Managing SSL certificates with ease
The lack of operational efficiencies and compliance pitfalls associated with poor SSL certificate management, and how the right SSL certificate management tool can help.