1
0
mirror of https://github.com/cookiengineer/audacity synced 2025-08-16 08:34:10 +02:00

Singleton CommandDirectory used to leak!

This commit is contained in:
Paul Licameli 2016-03-31 13:20:25 -04:00
parent 29349fedbb
commit fd8fa69c62
2 changed files with 8 additions and 21 deletions

View File

@ -32,13 +32,10 @@
#include "ImportExportCommands.h" #include "ImportExportCommands.h"
#include "OpenSaveCommands.h" #include "OpenSaveCommands.h"
CommandDirectory *CommandDirectory::mInstance = NULL; std::unique_ptr<CommandDirectory> CommandDirectory::mInstance;
CommandDirectory::CommandDirectory() CommandDirectory::CommandDirectory()
{ {
wxASSERT(mInstance == NULL);
mInstance = this;
// Create the command map. // Create the command map.
// Adding an entry here is the easiest way to register a Command class. // Adding an entry here is the easiest way to register a Command class.
AddCommand(new ScreenshotCommandType()); AddCommand(new ScreenshotCommandType());
@ -96,17 +93,7 @@ void CommandDirectory::AddCommand(CommandType *type)
CommandDirectory *CommandDirectory::Get() CommandDirectory *CommandDirectory::Get()
{ {
if (mInstance == NULL) if (!mInstance)
{ mInstance.reset(safenew CommandDirectory());
return new CommandDirectory(); return mInstance.get();
}
return mInstance;
}
void CommandDirectory::Destroy()
{
if (mInstance != NULL)
{
delete mInstance;
}
} }

View File

@ -23,6 +23,7 @@ they are kept separate to make things simpler.
#ifndef __COMMANDDIRECTORY__ #ifndef __COMMANDDIRECTORY__
#define __COMMANDDIRECTORY__ #define __COMMANDDIRECTORY__
#include "../MemoryX.h"
#include "CommandMisc.h" #include "CommandMisc.h"
#include "CommandType.h" #include "CommandType.h"
@ -32,10 +33,9 @@ class CommandOutputTarget;
class CommandDirectory class CommandDirectory
{ {
private: private:
static CommandDirectory *mInstance; static std::unique_ptr<CommandDirectory> mInstance;
CommandMap mCmdMap; CommandMap mCmdMap;
public: public:
CommandDirectory();
~CommandDirectory(); ~CommandDirectory();
/// If a command with the given name has been registered in the directory, /// If a command with the given name has been registered in the directory,
@ -49,8 +49,8 @@ public:
/// Get a pointer to the singleton instance /// Get a pointer to the singleton instance
static CommandDirectory *Get(); static CommandDirectory *Get();
/// Manually DELETE the singleton instance private:
static void Destroy(); CommandDirectory();
}; };
#endif /* End of include guard: __COMMANDDIRECTORY__ */ #endif /* End of include guard: __COMMANDDIRECTORY__ */