The Daily WTF
Follow
CodeSOD: A Single Lint Problem
The provided C++ code demonstrates a flawed singleton implementation used for utility functions. A CUtilities class is declared with utility methods and a global pointer, g_Utility. The constructor of CUtilities initializes g_Utility to point to the current instance. However, this setup doesn't prevent multiple instances, leading to memory leaks when new instances overwrite the pointer. An initialization class, CUtilityInit, was used to create an instance of CUtilities, thereby calling its constructor. The problem arises because the CUtilities member within CUtilityInit was marked as unused by a linter. An employee, Olivia, removed this unused member. This action broke the program because the global pointer g_Utility was never initialized. The correct fix involved directly initializing g_Utility with an instance at startup, rather than relying on the flawed singleton approach. The text emphasizes that singletons are generally a bad practice and poorly implemented ones create significant problems. Developers are advised to avoid using design patterns simply for the sake of using them.