Strictly Come Dancing 2025's first star is REVEALED: Gladiators star Nitro set to compete in new series as the show battles a string of scandals
Pete Wicks admits he's 'in love' with a new woman six months on from his romance with Maura Higgins
The International Obfuscated C Code Contest is back for 2024
The IOCCC, as it's familiarly known, is back after a four-year gap, giving the entrants more time to come up with some remarkably devious code.…
Khloe Kardashian enjoys hamper from Meghan Markle's As Ever brand - as delighted Duchess reposts reality star's surprising social media shoutout
Lioness Ella Toone shows off her sparkling engagement ring as she celebrates her partner Joe Bunney's romantic beach proposal with pals
My wife and I are in our 50s and haven't had sex for TWO YEARS. Today I'm revealing all about the desperate truth so many couples hide... by CHRIS TAUNTON
James Whale's final words: Radio star said the past few months had been hard and expressed his sadness at missing Christmas in posthumous column written weeks before his death
How many crimes are going unsolved in YOUR street? Find out with our search tool - as interactive map names and shames worst-performing police forces
Flight attendant reveals the holiday hotspot with the worst passengers: 'It was just absolute chaos'
Pay attention, class: Today you’ll learn the wrong way to turn things off
Who, Me? Welcome once more to Who, Me? It’s The Register’s Monday column in which we celebrate your SNAFUS and rejoice in your recoveries.…
Bride left in shock after receiving HR complaint from colleague for leaving her out of her wedding
Shoppers rush to major supermarket to shop huge clothing sale with items reduced to as little as 99p
I thought my 'vegan' sister had stopped posting her toddler on social media for privacy... really he was dead she and her 'religious' husband had buried him in their garden - this is my one regret
Actor Ray Brooks famous for the voice of 1970s TV character Mr Benn dies aged 86 after a short illness and dementia battle
Paul Gallagher joins Oasis star brother Liam on Edinburgh-bound private jet ahead of looming court date after being charged with rape and other sexual offences
British tourist targeted by masked Rolex rippers in Majorca who snatch £35,000 timepiece in front of his wife and child after holiday meal
Georgia Kousoulou says nothing was off limits when filming ITV2's Happy Essex After
AI companies Nvidia and AMD to pay 15% of China chip sales revenues to US
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.