From de4dfdad163abaff265adde0af4ff48e7424d7af Mon Sep 17 00:00:00 2001 From: simonb Date: Sat, 22 Apr 2006 12:16:38 +0000 Subject: fix static management of CommandParser, sf.net 1474444 --- ChangeLog | 3 +++ src/CommandParser.cc | 19 +++++++++++++++++-- src/CommandParser.hh | 5 ++++- 3 files changed, 24 insertions(+), 3 deletions(-) diff --git a/ChangeLog b/ChangeLog index 6eb863c..523a59a 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,6 +1,9 @@ (Format: Year/Month/Day) Changes for 0.9.16: *06/04/22: + * static CommandParser could be used after destruction (Simon) + Fixes sf.net #1474444, segfault when DISPLAY not set + CommmandParser.hh/cc * Minor cosmetic changes, thanks Semushin Slava (php-coder AT ngs ru) main.cc Screen.cc util/startfluxbox.in * First draft of new docs in docs/asciidoc (Mathias) diff --git a/src/CommandParser.cc b/src/CommandParser.cc index 6fcb771..1f8218e 100644 --- a/src/CommandParser.cc +++ b/src/CommandParser.cc @@ -32,6 +32,9 @@ using std::vector; using FbTk::StringUtil::removeFirstWhitespace; using FbTk::StringUtil::toLower; + +CommandParser *CommandParser::s_singleton = 0; + CommandFactory::CommandFactory() { } @@ -39,15 +42,24 @@ CommandFactory::CommandFactory() { CommandFactory::~CommandFactory() { // remove all associations with this factory CommandParser::instance().removeAssociation(*this); + } void CommandFactory::addCommand(const string &command_name) { CommandParser::instance().associateCommand(command_name, *this); } +// ensure it is singleton +CommandParser::CommandParser() { + if (s_singleton != 0) + throw std::string("CommandParser currently meant ot be singleton"); +} + CommandParser &CommandParser::instance() { - static CommandParser singleton; - return singleton; + if (s_singleton == 0) + s_singleton = new CommandParser(); + + return *s_singleton; } FbTk::Command *CommandParser::parseLine(const string &line) { @@ -105,4 +117,7 @@ void CommandParser::removeAssociation(CommandFactory &factory) { m_commandfactorys.erase(commands.back()); commands.pop_back(); } + + if (m_commandfactorys.empty()) + delete s_singleton; } diff --git a/src/CommandParser.hh b/src/CommandParser.hh index 10f47aa..5501345 100644 --- a/src/CommandParser.hh +++ b/src/CommandParser.hh @@ -54,6 +54,8 @@ public: /// @return parses and returns a command matching the line FbTk::Command *parseLine(const std::string &line); + CommandParser(); + /// @return instance of command parser static CommandParser &instance(); /// @return map of factorys @@ -71,7 +73,8 @@ private: const std::string &arguments); CommandFactoryMap m_commandfactorys; ///< a string to factory map - + + static CommandParser *s_singleton; }; #endif // COMMANDPARSER_HH -- cgit v0.11.2