From 0f0fc5f1987e879ddfffdb0de9214987157eedc2 Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Sun, 20 Jun 2010 11:37:47 +0000 Subject: [PATCH] 2010-06-20 Tatsuhiro Tsujikawa Use auto_delete_container to delete created Command when exception is thrown rather than deleting them in catch block. * src/AbstractCommand.cc * src/CheckIntegrityCommand.cc * src/DHTSetup.cc * src/FileAllocationCommand.cc * src/TrackerWatcherCommand.cc * src/a2functional.h --- ChangeLog | 11 +++++++++++ src/AbstractCommand.cc | 13 +++++-------- src/CheckIntegrityCommand.cc | 26 ++++++++++---------------- src/DHTSetup.cc | 22 ++++++++++++---------- src/FileAllocationCommand.cc | 13 +++++-------- src/TrackerWatcherCommand.cc | 16 ++++++++-------- src/a2functional.h | 18 +++++++++++++++++- 7 files changed, 68 insertions(+), 51 deletions(-) diff --git a/ChangeLog b/ChangeLog index ba378926..4a34037d 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,14 @@ +2010-06-20 Tatsuhiro Tsujikawa + + Use auto_delete_container to delete created Command when exception + is thrown rather than deleting them in catch block. + * src/AbstractCommand.cc + * src/CheckIntegrityCommand.cc + * src/DHTSetup.cc + * src/FileAllocationCommand.cc + * src/TrackerWatcherCommand.cc + * src/a2functional.h + 2010-06-20 Tatsuhiro Tsujikawa Removed BDE and bencode diff --git a/src/AbstractCommand.cc b/src/AbstractCommand.cc index 03fa6476..e8bd53c3 100644 --- a/src/AbstractCommand.cc +++ b/src/AbstractCommand.cc @@ -722,14 +722,11 @@ void AbstractCommand::prepareForNextAction(Command* nextCommand) SharedHandle entry (new StreamCheckIntegrityEntry(_requestGroup, nextCommand)); - std::vector commands; - try { - _requestGroup->processCheckIntegrityEntry(commands, entry, _e); - } catch(RecoverableException& e) { - std::for_each(commands.begin(), commands.end(), Deleter()); - throw; - } - _e->addCommand(commands); + std::vector* commands = new std::vector(); + auto_delete_container > commandsDel(commands); + _requestGroup->processCheckIntegrityEntry(*commands, entry, _e); + _e->addCommand(*commands); + commands->clear(); _e->setNoWait(true); } diff --git a/src/CheckIntegrityCommand.cc b/src/CheckIntegrityCommand.cc index 262f2626..72087520 100644 --- a/src/CheckIntegrityCommand.cc +++ b/src/CheckIntegrityCommand.cc @@ -75,26 +75,20 @@ bool CheckIntegrityCommand::executeInternal() getLogger()->notice (MSG_VERIFICATION_SUCCESSFUL, getRequestGroup()->getDownloadContext()->getBasePath().c_str()); - std::vector commands; - try { - _entry->onDownloadFinished(commands, getDownloadEngine()); - } catch(RecoverableException& e) { - std::for_each(commands.begin(), commands.end(), Deleter()); - throw; - } - getDownloadEngine()->addCommand(commands); + std::vector* commands = new std::vector(); + auto_delete_container > commandsDel(commands); + _entry->onDownloadFinished(*commands, getDownloadEngine()); + getDownloadEngine()->addCommand(*commands); + commands->clear(); } else { getLogger()->error (MSG_VERIFICATION_FAILED, getRequestGroup()->getDownloadContext()->getBasePath().c_str()); - std::vector commands; - try { - _entry->onDownloadIncomplete(commands, getDownloadEngine()); - } catch(RecoverableException& e) { - std::for_each(commands.begin(), commands.end(), Deleter()); - throw; - } - getDownloadEngine()->addCommand(commands); + std::vector* commands = new std::vector(); + auto_delete_container > commandsDel(commands); + _entry->onDownloadIncomplete(*commands, getDownloadEngine()); + getDownloadEngine()->addCommand(*commands); + commands->clear(); } getDownloadEngine()->setNoWait(true); return true; diff --git a/src/DHTSetup.cc b/src/DHTSetup.cc index 3c21fd78..2db5189f 100644 --- a/src/DHTSetup.cc +++ b/src/DHTSetup.cc @@ -91,8 +91,9 @@ void DHTSetup::setup(std::vector& commands, DownloadEngine* e) if(_initialized) { return; } - std::vector tempCommands; try { + std::vector* tempCommands = new std::vector(); + auto_delete_container > commandsDel(tempCommands); // load routing table and localnode id here SharedHandle localNode; @@ -219,7 +220,7 @@ void DHTSetup::setup(std::vector& commands, DownloadEngine* e) command->setTaskFactory(taskFactory); command->setRoutingTable(routingTable); command->setLocalNode(localNode); - tempCommands.push_back(command); + tempCommands->push_back(command); } } else { _logger->info("No DHT entry point specified."); @@ -231,13 +232,13 @@ void DHTSetup::setup(std::vector& commands, DownloadEngine* e) command->setMessageReceiver(receiver); command->setTaskQueue(taskQueue); command->setReadCheckSocket(connection->getSocket()); - tempCommands.push_back(command); + tempCommands->push_back(command); } { DHTTokenUpdateCommand* command = new DHTTokenUpdateCommand(e->newCUID(), e, DHT_TOKEN_UPDATE_INTERVAL); command->setTokenTracker(tokenTracker); - tempCommands.push_back(command); + tempCommands->push_back(command); } { DHTBucketRefreshCommand* command = @@ -246,28 +247,29 @@ void DHTSetup::setup(std::vector& commands, DownloadEngine* e) command->setTaskQueue(taskQueue); command->setRoutingTable(routingTable); command->setTaskFactory(taskFactory); - tempCommands.push_back(command); + tempCommands->push_back(command); } { DHTPeerAnnounceCommand* command = new DHTPeerAnnounceCommand(e->newCUID(), e, DHT_PEER_ANNOUNCE_CHECK_INTERVAL); command->setPeerAnnounceStorage(peerAnnounceStorage); - tempCommands.push_back(command); + tempCommands->push_back(command); } { DHTAutoSaveCommand* command = new DHTAutoSaveCommand(e->newCUID(), e, 30*60); command->setLocalNode(localNode); command->setRoutingTable(routingTable); - tempCommands.push_back(command); + tempCommands->push_back(command); } _initialized = true; - commands.insert(commands.end(), tempCommands.begin(), tempCommands.end()); + commands.insert(commands.end(), tempCommands->begin(), tempCommands->end()); + tempCommands->clear(); } catch(RecoverableException& e) { - _logger->error("Exception caught while initializing DHT functionality. DHT is disabled.", e); + _logger->error("Exception caught while initializing DHT functionality." + " DHT is disabled.", e); DHTRegistry::clearData(); - std::for_each(tempCommands.begin(), tempCommands.end(), Deleter()); } } diff --git a/src/FileAllocationCommand.cc b/src/FileAllocationCommand.cc index 04bcc377..eeb85a96 100644 --- a/src/FileAllocationCommand.cc +++ b/src/FileAllocationCommand.cc @@ -76,14 +76,11 @@ bool FileAllocationCommand::executeInternal() } getDownloadEngine()->getFileAllocationMan()->dropPickedEntry(); - std::vector commands; - try { - _fileAllocationEntry->prepareForNextAction(commands, getDownloadEngine()); - } catch(RecoverableException& e) { - std::for_each(commands.begin(), commands.end(), Deleter()); - throw; - } - getDownloadEngine()->addCommand(commands); + std::vector* commands = new std::vector(); + auto_delete_container > commandsDel(commands); + _fileAllocationEntry->prepareForNextAction(*commands, getDownloadEngine()); + getDownloadEngine()->addCommand(*commands); + commands->clear(); getDownloadEngine()->setNoWait(true); return true; } else { diff --git a/src/TrackerWatcherCommand.cc b/src/TrackerWatcherCommand.cc index 09a29bf0..517f13ad 100644 --- a/src/TrackerWatcherCommand.cc +++ b/src/TrackerWatcherCommand.cc @@ -104,17 +104,17 @@ bool TrackerWatcherCommand::execute() { if(_trackerRequestGroup.isNull()) { _trackerRequestGroup = createAnnounce(); if(!_trackerRequestGroup.isNull()) { - std::vector commands; try { - _trackerRequestGroup->createInitialCommand(commands, _e); + std::vector* commands = new std::vector(); + auto_delete_container > commandsDel(commands); + _trackerRequestGroup->createInitialCommand(*commands, _e); + _e->addCommand(*commands); + commands->clear(); + if(getLogger()->debug()) { + getLogger()->debug("added tracker request command"); + } } catch(RecoverableException& ex) { getLogger()->error(EX_EXCEPTION_CAUGHT, ex); - std::for_each(commands.begin(), commands.end(), Deleter()); - commands.clear(); - } - _e->addCommand(commands); - if(getLogger()->debug()) { - getLogger()->debug("added tracker request command"); } } } else if(_trackerRequestGroup->downloadFinished()){ diff --git a/src/a2functional.h b/src/a2functional.h index a4e7e7fe..2e83667e 100644 --- a/src/a2functional.h +++ b/src/a2functional.h @@ -36,9 +36,11 @@ #define _D_A2_FUNCTIONAL_H_ #include +#include +#include + #include "SharedHandle.h" #include "A2STR.h" -#include namespace aria2 { @@ -204,6 +206,20 @@ public: } }; +template +class auto_delete_container { +private: + Container* _c; +public: + auto_delete_container(Container* c):_c(c) {} + + ~auto_delete_container() + { + std::for_each(_c->begin(), _c->end(), Deleter()); + delete _c; + } +}; + template std::string strjoin(InputIterator first, InputIterator last, const DelimiterType& delim)