Computer
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.How 12 'Enola Gay' Crew Members Remember Dropping the Atomic Bomb
Read more of this story at Slashdot.
How Python is Fighting Open Source's 'Phantom' Dependencies Problem
Read more of this story at Slashdot.
$1M Stolen in 'Industrial-Scale Crypto Theft' Using AI-Generated Code
Read more of this story at Slashdot.
Autonomous AI-Guided Black Hawk Helicopter Tested to Fight Wildfires
Read more of this story at Slashdot.
Astrophysicist Proposes Paperclip-Sized Spacecraft Could Travel at Lightspeed to a Black Hole
Read more of this story at Slashdot.
WSJ Finds 'Dozens' of Delusional Claims from AI Chats as Companies Scramble for a Fix
Read more of this story at Slashdot.
As Electric Bills Rise, Evidence Mounts That U.S. Data Centers Share Blame
Read more of this story at Slashdot.
Meteorite That Hit Home Is Older Than Earth, Scientists Say
Read more of this story at Slashdot.
KDE Calls Microsoft's Copilot Key 'Dumb', Will Let You Remap It Soon
Read more of this story at Slashdot.
A Huge $2 Billion 'Solar + Storage' Project in California Powers Up
Read more of this story at Slashdot.
Rust's Annual Tech Report: Trusted Publishing for Packages and a C++/Rust Interop Strategy
Read more of this story at Slashdot.
Microsoft Sued Over Plans to Discontinue Windows 10 Support
Read more of this story at Slashdot.
AOL Finally Discontinues Its Dial-Up Internet Access - After 34 Years
Read more of this story at Slashdot.
'Hour of Code' Announces It's Now Evolving Into 'Hour of AI'
Read more of this story at Slashdot.
SpaceX's Crew-10 Astronauts Return to Earth After Nearly 5 months in Space
Read more of this story at Slashdot.
Linus Torvalds Rejects RISC-V Changes For Linux 6.17 For Being Late and 'Garbage'
Read more of this story at Slashdot.
Google Says Its AI-Based Bug Hunter Found 20 Security Vulnerabilities
Read more of this story at Slashdot.
Strange Wild Pigs in California - What Turned Their Flesh Blue?
Read more of this story at Slashdot.
Initiative Seeks AI Lab to Build 'American Truly Open Models' (ATOM)
Read more of this story at Slashdot.