Inside one of Britain's most notorious gangster families... and how even an SAS trained hitman couldn't take them out
CodeSOD: A Single Lint Problem
We've discussed singleton abuse as an antipattern many times on this site, but folks keep trying to find new ways to implement them badly. And Olivia's co-worker certainly found one.
We start with a C++ utility class with a bunch of functions in it:
//utilities.h class CUtilities { public CUtilities(); void doSomething(); void doSomeOtherThing(); }; extern CUtilities* g_Utility;So yes, if you're making a pile of utility methods, or if you want a singleton object, the keyword you're looking for is static. We'll set that aside. This class declares a class, and then also declares that there will be a pointer to the class, somewhere.
We don't have to look far.
//utilities.cpp CUtilities* g_Utility = nullptr; CUtilities::CUtilities() { g_Utility = this; } // all my do-whatever functions hereThis defines the global pointer variable, and then also writes the constructor of the utility class so that it initializes the global pointer to itself.
It's worth noting, at this point, that this is not a singleton, because this does nothing to prevent multiple instances from being created. What it does guarantee is that for each new instance, we overwrite g_Utility without disposing of what was already in there, which is a nice memory leak.
But where, or where, does the constructor get called?
//startup.h class CUtilityInit { private: CUtilities m_Instance; }; //startup.cpp CUtilityInit *utils = new CUtilityInit();I don't hate a program that starts with an initialization step that clearly instantiates all the key objects. There's just one little problem here that we'll come back to in just a moment, but let's look at the end result.
Anywhere that needs the utilities now can do this:
#include "utilities.h" //in the code g_Utility->doSomething();There's just one key problem: back in the startup.h, we have a private member called CUtilities m_Instance which is never referenced anywhere else in the code. This means when people, like Olivia, are trawling through the codebase looking for linter errors they can fix, they may see an "unused member" and decide to remove it. Which is what Olivia did.
The result compiles just fine, but explodes at runtime since g_Utility was never initialized.
The fix was simple: just don't try and make this a singleton, since it isn't one anyway. At startup, she just populated g_Utility with an instance, and threw away all the weird code around populating it through construction.
Singletons are, as a general rule, bad. Badly implemented singletons themselves easily turn into landmines waiting for unwary developers. Stop being clever and don't try and apply a design pattern for the sake of saying you used a design pattern.
.comment { border: none; } [Advertisement] Picking up NuGet is easy. Getting good at it takes time. Download our guide to learn the best practice of NuGet for the Enterprise.China says its lunar lander passed Luna-landing and take-off tests
China’s Manned Space Engineering Network says the country’s first crewed lunar lander last week completed a comprehensive landing and takeoff verification test, bringing it closer to landing on Luna - and leaving it again afterwards.…
Virgil van Dijk hits out at Crystal Palace fans who spoiled minute's silence for Diogo Jota before Liverpool's Community Shield defeat by Crystal Palace at Wembley
Princess Andre's childhood trauma: Katie Price's daughter says she 'saw things kids shouldn't see' as she lays bare her mother's 'problems' and how she has a counsellor
Here comes the sun again... UK set to be hotter than Barbados as Britain sizzles in 34C heatwave
Thinking of taking fat jabs on holiday? These are the rules you MUST know before you set off: LOUISE ATKINSON
Is Prince Louis following in Prince Harry's comedic footsteps? The young royal shares a sense of humour with the Duke of Sussex and the late Queen
DR MAX PEMBERTON: Meghan's deathly silence on Harry's woes says so much about their relationship. This is not what a successful marriage looks like...
King Charles' testy encounter with an award-winning gardener who he 'summoned to Highgrove'
The Essex village that's 'slowly moving on' after family murdered 40 years ago
Olly Murs says 'we've been trying for years' as he offers insight into Mark Wright friendship
Israeli PM Benjamin Netanyahu claims Gaza City expansion plan is 'best way to end the war'
Google fixing Gemini so it doesn't channel paranoid androids quite so often
Google is aware that its Gemini AI chatbot can sometimes castigate itself harshly for failing to solve a problem and plans to fix it.…
How 12 'Enola Gay' Crew Members Remember Dropping the Atomic Bomb
Read more of this story at Slashdot.