Close Menu
    DevStackTipsDevStackTips
    • Home
    • News & Updates
      1. Tech & Work
      2. View All

      Designing With AI, Not Around It: Practical Advanced Techniques For Product Design Use Cases

      August 11, 2025

      Why Companies Are Investing in AI-Powered React.js Development Services in 2025

      August 11, 2025

      The coming AI smartphone: Redefining personal tech

      August 11, 2025

      Modern React animation libraries: Real examples for engaging UIs

      August 11, 2025

      How Debian 13’s little improvements add up to the distro’s surprisingly big leap forward

      August 11, 2025

      Why xAI is giving you ‘limited’ free access to Grok 4

      August 11, 2025

      How Apple may revamp Siri to a voice assistant I’d actually use (and ditch Gemini for)

      August 11, 2025

      I jump-started a bus from the 1930s with this power bank – here’s the verdict

      August 11, 2025
    • Development
      1. Algorithms & Data Structures
      2. Artificial Intelligence
      3. Back-End Development
      4. Databases
      5. Front-End Development
      6. Libraries & Frameworks
      7. Machine Learning
      8. Security
      9. Software Engineering
      10. Tools & IDEs
      11. Web Design
      12. Web Development
      13. Web Security
      14. Programming Languages
        • PHP
        • JavaScript
      Featured

      Laravel’s UsePolicy Attribute: Explicit Authorization Control

      August 11, 2025
      Recent

      Laravel’s UsePolicy Attribute: Explicit Authorization Control

      August 11, 2025

      The Laravel Way to Build AI Agents That Actually Work

      August 11, 2025

      The Laravel Way to Build AI Agents That Actually Work

      August 11, 2025
    • Operating Systems
      1. Windows
      2. Linux
      3. macOS
      Featured

      Microsoft sued over killing support for Windows 10

      August 11, 2025
      Recent

      Microsoft sued over killing support for Windows 10

      August 11, 2025

      Grok 4 rolled out for free-tier users worldwide, with some limits

      August 11, 2025

      Firefox AI slammed for hogging CPU and draining battery

      August 11, 2025
    • Learning Resources
      • Books
      • Cheatsheets
      • Tutorials & Guides
    Home»News & Updates»CodeSOD: A Single Lint Problem

    CodeSOD: A Single Lint Problem

    August 11, 2025

    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 here
    

    This 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.

    [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.

    Source: Read More 

    Facebook Twitter Reddit Email Copy Link
    Previous ArticleASRock Industrial NUC BOX-255H Running Linux: Introduction to the series
    Next Article Maloja – simple self-hosted music scrobble database

    Related Posts

    News & Updates

    How Debian 13’s little improvements add up to the distro’s surprisingly big leap forward

    August 11, 2025
    News & Updates

    Why xAI is giving you ‘limited’ free access to Grok 4

    August 11, 2025
    Leave A Reply Cancel Reply

    For security, use of Google's reCAPTCHA service is required which is subject to the Google Privacy Policy and Terms of Use.

    Continue Reading

    This Amazon Echo Frames deal comes with an Echo Spot (and it’s cheaper than Meta Ray-Bans)

    News & Updates

    This GaN charger is a cross between a yo-yo and a tape measure – and goes with me everywhere

    News & Updates

    I never thought I’d praise a kickstand power bank – until I tried this one

    News & Updates

    OpenAI wins gold at prestigious math competition – why that matters more than you think

    News & Updates

    Highlights

    News & Updates

    I played Capcom’s Pragmata, and it’s not what you expect

    June 12, 2025

    The long-dormant Pragmata emerged during Summer Game Fest 2025, and after going hands-on with this…

    Customize Logseq With Themes and Plugins

    April 22, 2025

    Battlefield 6 is the official title, and its reveal trailer has a release date this week — is my favorite multiplayer FPS about to make a massive comeback?

    July 22, 2025

    I missed out on the Clair Obscur: Expedition 33 Collector’s Edition but thankfully, the developers are launching something special

    May 22, 2025
    © DevStackTips 2025. All rights reserved.
    • Contact
    • Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.