fix static management of CommandParser, sf.net 1474444
simonb simonb
3 files changed,
24 insertions(+),
3 deletions(-)
M
ChangeLog
→
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)
M
src/CommandParser.cc
→
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() { // 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 @@ while (!commands.empty()) {
m_commandfactorys.erase(commands.back()); commands.pop_back(); } + + if (m_commandfactorys.empty()) + delete s_singleton; }
M
src/CommandParser.hh
→
src/CommandParser.hh
@@ -54,6 +54,8 @@
/// @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 @@ FbTk::Command *toCommand(const std::string &command,
const std::string &arguments); CommandFactoryMap m_commandfactorys; ///< a string to factory map - + + static CommandParser *s_singleton; }; #endif // COMMANDPARSER_HH