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

      Coded Smorgasbord: High Strung

      September 26, 2025

      Chainguard launches trusted collection of verified JavaScript libraries

      September 26, 2025

      CData launches Connect AI to provide agents access to enterprise data sources

      September 26, 2025

      PostgreSQL 18 adds asynchronous I/O to improve performance

      September 26, 2025

      Distribution Release: Neptune 9.0

      September 25, 2025

      Distribution Release: Kali Linux 2025.3

      September 23, 2025

      Distribution Release: SysLinuxOS 13

      September 23, 2025

      Development Release: MX Linux 25 Beta 1

      September 22, 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

      PHP 8.5.0 RC 1 available for testing

      September 26, 2025
      Recent

      PHP 8.5.0 RC 1 available for testing

      September 26, 2025

      Terraform Code Generator Using Ollama and CodeGemma

      September 26, 2025

      Beyond Denial: How AI Concierge Services Can Transform Healthcare from Reactive to Proactive

      September 25, 2025
    • Operating Systems
      1. Windows
      2. Linux
      3. macOS
      Featured

      Distribution Release: Neptune 9.0

      September 25, 2025
      Recent

      Distribution Release: Neptune 9.0

      September 25, 2025

      FOSS Weekly #25.39: Kill Switch Phones, LMDE 7, Zorin OS 18 Beta, Polybar, Apt History and More Linux Stuff

      September 25, 2025

      Distribution Release: Kali Linux 2025.3

      September 23, 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

    Distribution Release: Neptune 9.0

    September 25, 2025
    News & Updates

    Distribution Release: Kali Linux 2025.3

    September 23, 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

    CSS Processing Guide

    Web Development

    CVE-2025-5117 – WordPress Property Plugin Privilege Escalation Vulnerability

    Common Vulnerabilities and Exposures (CVEs)

    CVE-2025-53906 – Vim Zip File Path Traversal Vulnerability

    Common Vulnerabilities and Exposures (CVEs)

    This new YouTube Shorts feature lets you circle to search videos more easily

    News & Updates

    Highlights

    CVE-2025-50062 – Oracle PeopleSoft Global Payroll Core HTTP Low Privilege Remote Unauthorized Access and Data Modification Vulnerability

    July 16, 2025

    CVE ID : CVE-2025-50062

    Published : July 15, 2025, 8:15 p.m. | 6 hours, 44 minutes ago

    Description : Vulnerability in the PeopleSoft Enterprise HCM Global Payroll Core product of Oracle PeopleSoft (component: Global Payroll for Core). Supported versions that are affected are 9.2.51 and 9.2.52. Easily exploitable vulnerability allows low privileged attacker with network access via HTTP to compromise PeopleSoft Enterprise HCM Global Payroll Core. Successful attacks of this vulnerability can result in unauthorized creation, deletion or modification access to critical data or all PeopleSoft Enterprise HCM Global Payroll Core accessible data as well as unauthorized access to critical data or complete access to all PeopleSoft Enterprise HCM Global Payroll Core accessible data. CVSS 3.1 Base Score 8.1 (Confidentiality and Integrity impacts). CVSS Vector: (CVSS:3.1/AV:N/AC:L/PR:L/UI:N/S:U/C:H/I:H/A:N).

    Severity: 8.1 | HIGH

    Visit the link for more details, such as CVSS details, affected products, timeline, and more…

    Buy a Samsung Galaxy Watch 7 and get a free SmartTag2 Bluetooth tracker – here’s how

    May 27, 2025

    CVE-2025-43928 – Infodraw Media Relay Service File Reading Vulnerability

    April 20, 2025

    CVE-2023-41566 – OA EKP Arbitrary Download Vulnerability

    July 17, 2025
    © DevStackTips 2025. All rights reserved.
    • Contact
    • Privacy Policy

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