Fixes resource leaks and removes deprecated c_str() calls where possible

This commit is contained in:
DennisOSRM 2013-10-13 14:13:08 +02:00
parent 2a1c24763d
commit 457519eae3
3 changed files with 63 additions and 31 deletions

View File

@ -21,32 +21,37 @@ or see http://www.gnu.org/licenses/agpl.txt.
#include "QueryObjectsStorage.h" #include "QueryObjectsStorage.h"
QueryObjectsStorage::QueryObjectsStorage(boost::unordered_map<const std::string,boost::filesystem::path>& paths) { QueryObjectsStorage::QueryObjectsStorage( const ServerPaths & paths ) {
if( paths["hsgrdata"].empty() ) { if( paths.find("hsgrdata") == paths.end() ) {
throw OSRMException("no hsgr file given in ini file"); throw OSRMException("no hsgr file given in ini file");
} }
if( paths["ramindex"].empty() ) { if( paths.find("ramindex") == paths.end() ) {
throw OSRMException("no ram index file given in ini file"); throw OSRMException("no ram index file given in ini file");
} }
if( paths["fileindex"].empty() ) { if( paths.find("fileindex") == paths.end() ) {
throw OSRMException("no mem index file given in ini file"); throw OSRMException("no mem index file given in ini file");
} }
if( paths["nodesdata"].empty() ) { if( paths.find("nodesdata") == paths.end() ) {
throw OSRMException("no nodes file given in ini file"); throw OSRMException("no nodes file given in ini file");
} }
if( paths["edgesdata"].empty() ) { if( paths.find("edgesdata") == paths.end() ) {
throw OSRMException("no edges file given in ini file"); throw OSRMException("no edges file given in ini file");
} }
if( paths["namesdata"].empty() ) { if( paths.find("namesdata") == paths.end() ) {
throw OSRMException("no names file given in ini file"); throw OSRMException("no names file given in ini file");
} }
SimpleLogger().Write() << "loading graph data"; SimpleLogger().Write() << "loading graph data";
//Deserialize road network graph //Deserialize road network graph
ServerPaths::const_iterator paths_iterator = paths.find("hsgrdata");
BOOST_ASSERT(paths.end() != paths_iterator);
const std::string & hsgr_data_string = paths_iterator->second.string();
std::vector< QueryGraph::_StrNode> node_list; std::vector< QueryGraph::_StrNode> node_list;
std::vector< QueryGraph::_StrEdge> edge_list; std::vector< QueryGraph::_StrEdge> edge_list;
const int number_of_nodes = readHSGRFromStream( const int number_of_nodes = readHSGRFromStream(
paths["hsgrdata"].c_str(), hsgr_data_string,
node_list, node_list,
edge_list, edge_list,
&check_sum &check_sum
@ -54,18 +59,22 @@ QueryObjectsStorage::QueryObjectsStorage(boost::unordered_map<const std::string,
SimpleLogger().Write() << "Data checksum is " << check_sum; SimpleLogger().Write() << "Data checksum is " << check_sum;
graph = new QueryGraph(node_list, edge_list); graph = new QueryGraph(node_list, edge_list);
assert(0 == node_list.size()); BOOST_ASSERT(0 == node_list.size());
assert(0 == edge_list.size()); BOOST_ASSERT(0 == edge_list.size());
if(!paths["timestamp"].empty()) { paths_iterator = paths.find("hsgrdata");
if(paths.end() != paths_iterator) {
SimpleLogger().Write() << "Loading Timestamp"; SimpleLogger().Write() << "Loading Timestamp";
std::ifstream timestampInStream(paths["timestamp"].c_str()); const std::string & timestamp_string = paths_iterator->second.string();
if(!timestampInStream) {
SimpleLogger().Write(logWARNING) << paths["timestamp"].c_str() << " not found"; boost::filesystem::ifstream time_stamp_instream(timestamp_string);
if( !time_stamp_instream.good() ) {
SimpleLogger().Write(logWARNING) << timestamp_string << " not found";
} }
getline(timestampInStream, timestamp); getline(time_stamp_instream, timestamp);
timestampInStream.close(); time_stamp_instream.close();
} }
if(!timestamp.length()) { if(!timestamp.length()) {
timestamp = "n/a"; timestamp = "n/a";
@ -75,12 +84,26 @@ QueryObjectsStorage::QueryObjectsStorage(boost::unordered_map<const std::string,
} }
SimpleLogger().Write() << "Loading auxiliary information"; SimpleLogger().Write() << "Loading auxiliary information";
paths_iterator = paths.find("ramindex");
BOOST_ASSERT(paths.end() != paths_iterator);
const std::string & ram_index_string = paths_iterator->second.string();
paths_iterator = paths.find("fileindex");
BOOST_ASSERT(paths.end() != paths_iterator);
const std::string & file_index_string = paths_iterator->second.string();
paths_iterator = paths.find("nodesdata");
BOOST_ASSERT(paths.end() != paths_iterator);
const std::string & nodes_data_string = paths_iterator->second.string();
paths_iterator = paths.find("edgesdata");
BOOST_ASSERT(paths.end() != paths_iterator);
const std::string & edges_data_string = paths_iterator->second.string();
//Init nearest neighbor data structure //Init nearest neighbor data structure
nodeHelpDesk = new NodeInformationHelpDesk( nodeHelpDesk = new NodeInformationHelpDesk(
paths["ramindex"].c_str(), ram_index_string,
paths["fileindex"].c_str(), file_index_string,
paths["nodesdata"].c_str(), nodes_data_string,
paths["edgesdata"].c_str(), edges_data_string,
number_of_nodes, number_of_nodes,
check_sum check_sum
); );
@ -88,14 +111,17 @@ QueryObjectsStorage::QueryObjectsStorage(boost::unordered_map<const std::string,
//deserialize street name list //deserialize street name list
SimpleLogger().Write() << "Loading names index"; SimpleLogger().Write() << "Loading names index";
if ( !boost::filesystem::exists( paths["namesdata"] ) ) { paths_iterator = paths.find("namesdata");
BOOST_ASSERT(paths.end() != paths_iterator);
const std::string & names_data_string = paths_iterator->second.string();
if ( !boost::filesystem::exists( paths_iterator->second ) ) {
throw OSRMException("names file does not exist"); throw OSRMException("names file does not exist");
} }
if ( 0 == boost::filesystem::file_size( paths["namesdata"] ) ) { if ( 0 == boost::filesystem::file_size( paths_iterator->second ) ) {
throw OSRMException("names file is empty"); throw OSRMException("names file is empty");
} }
boost::filesystem::ifstream name_stream(paths["namesdata"], std::ios::binary); boost::filesystem::ifstream name_stream(names_data_string, std::ios::binary);
unsigned size = 0; unsigned size = 0;
name_stream.read((char *)&size, sizeof(unsigned)); name_stream.read((char *)&size, sizeof(unsigned));
BOOST_ASSERT_MSG(0 != size, "name file broken"); BOOST_ASSERT_MSG(0 != size, "name file broken");

View File

@ -24,6 +24,7 @@ or see http://www.gnu.org/licenses/agpl.txt.
#include "../../Util/GraphLoader.h" #include "../../Util/GraphLoader.h"
#include "../../Util/OSRMException.h" #include "../../Util/OSRMException.h"
#include "../../Util/ProgramOptions.h"
#include "../../Util/SimpleLogger.h" #include "../../Util/SimpleLogger.h"
#include "../../DataStructures/NodeInformationHelpDesk.h" #include "../../DataStructures/NodeInformationHelpDesk.h"
#include "../../DataStructures/QueryEdge.h" #include "../../DataStructures/QueryEdge.h"
@ -48,9 +49,9 @@ struct QueryObjectsStorage {
std::string timestamp; std::string timestamp;
unsigned check_sum; unsigned check_sum;
void GetName(const unsigned name_id, std::string & result) const; void GetName( const unsigned name_id, std::string & result ) const;
QueryObjectsStorage(boost::unordered_map<const std::string,boost::filesystem::path>& paths); QueryObjectsStorage( const ServerPaths & paths );
~QueryObjectsStorage(); ~QueryObjectsStorage();
}; };

View File

@ -49,7 +49,7 @@ namespace boost {
// exists. The validate() function must be defined in the same namespace // exists. The validate() function must be defined in the same namespace
// as the target type, (boost::filesystem::path in this case), otherwise // as the target type, (boost::filesystem::path in this case), otherwise
// it is not be called // it is not be called
void validate( inline void validate(
boost::any & v, boost::any & v,
const std::vector<std::string> & values, const std::vector<std::string> & values,
boost::filesystem::path *, boost::filesystem::path *,
@ -86,7 +86,7 @@ inline void PrepareConfigFile(
// generate boost::program_options object for the routing part // generate boost::program_options object for the routing part
bool GenerateServerProgramOptions( inline bool GenerateServerProgramOptions(
const int argc, const int argc,
const char * argv[], const char * argv[],
ServerPaths & paths, ServerPaths & paths,
@ -204,13 +204,18 @@ bool GenerateServerProgramOptions(
boost::program_options::notify(option_variables); boost::program_options::notify(option_variables);
// parse config file // parse config file
if(boost::filesystem::is_regular_file(paths["config"])) { ServerPaths::const_iterator path_iterator = paths.find("config");
if( path_iterator != paths.end() &&
boost::filesystem::is_regular_file(path_iterator->second)) {
SimpleLogger().Write() << SimpleLogger().Write() <<
"Reading options from: " << paths["config"].c_str(); "Reading options from: " << path_iterator->second.string();
std::string config_str; std::string config_str;
PrepareConfigFile( paths["config"], config_str ); PrepareConfigFile( paths["config"], config_str );
std::stringstream config_stream( config_str ); std::stringstream config_stream( config_str );
boost::program_options::store(parse_config_file(config_stream, config_file_options), option_variables); boost::program_options::store(
parse_config_file(config_stream, config_file_options),
option_variables
);
boost::program_options::notify(option_variables); boost::program_options::notify(option_variables);
} }
@ -218,7 +223,7 @@ bool GenerateServerProgramOptions(
if(!option_variables.count("base")) { if(!option_variables.count("base")) {
throw OSRMException("hsgrdata (or base) must be specified"); throw OSRMException("hsgrdata (or base) must be specified");
} }
paths["hsgrdata"] = std::string( paths["base"].c_str()) + ".hsgr"; paths["hsgrdata"] = std::string( paths["base"].string()) + ".hsgr";
} }
if(!option_variables.count("nodesdata")) { if(!option_variables.count("nodesdata")) {