widening a bit smart pointers usage considering xml data.

This commit is contained in:
David Carlier 2022-03-12 07:14:43 +00:00
parent 18018a5cc1
commit 9fb568eaa7
4 changed files with 6 additions and 18 deletions

View File

@ -449,7 +449,7 @@ void AddonsManager::loadInstalledAddons()
Log::info("addons", "Loading an xml file for installed addons: %s.", Log::info("addons", "Loading an xml file for installed addons: %s.",
m_file_installed.c_str()); m_file_installed.c_str());
} }
const XMLNode *xml = file_manager->createXMLTree(m_file_installed); auto xml = std::unique_ptr<XMLNode>(file_manager->createXMLTree(m_file_installed));
if(!xml) if(!xml)
return; return;
@ -463,8 +463,6 @@ void AddonsManager::loadInstalledAddons()
m_addons_list.getData().push_back(addon); m_addons_list.getData().push_back(addon);
} }
} // for i <= xml->getNumNodes() } // for i <= xml->getNumNodes()
delete xml;
} // loadInstalledAddons } // loadInstalledAddons
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------

View File

@ -42,14 +42,13 @@ MusicInformation *MusicInformation::create(const std::string &filename)
{ {
assert(filename.size() > 0); assert(filename.size() > 0);
XMLNode* root = file_manager->createXMLTree(filename); auto root = std::unique_ptr<XMLNode>(file_manager->createXMLTree(filename));
if (!root) return NULL; if (!root) return NULL;
if(root->getName()!="music") if(root->getName()!="music")
{ {
Log::error("MusicInformation", Log::error("MusicInformation",
"Music file '%s' does not contain music node.\n", "Music file '%s' does not contain music node.\n",
filename.c_str()); filename.c_str());
delete root;
return NULL; return NULL;
} }
std::string s; std::string s;
@ -61,11 +60,9 @@ MusicInformation *MusicInformation::create(const std::string &filename)
"One of 'title' or 'file' attribute " "One of 'title' or 'file' attribute "
"is missing in the music XML file '%s'!\n", "is missing in the music XML file '%s'!\n",
filename.c_str()); filename.c_str());
delete root;
return NULL; return NULL;
} }
MusicInformation *mi = new MusicInformation(root, filename); MusicInformation *mi = new MusicInformation(root.get(), filename);
delete root;
return mi; return mi;
} // create() } // create()

View File

@ -669,12 +669,11 @@ UserConfig::~UserConfig()
bool UserConfig::loadConfig() bool UserConfig::loadConfig()
{ {
const std::string filename = file_manager->getUserConfigFile(m_filename); const std::string filename = file_manager->getUserConfigFile(m_filename);
XMLNode* root = file_manager->createXMLTree(filename); auto root = std::unique_ptr<XMLNode>(file_manager->createXMLTree(filename));
if(!root || root->getName() != "stkconfig") if(!root || root->getName() != "stkconfig")
{ {
Log::info("UserConfig", Log::info("UserConfig",
"Could not read user config file '%s'. A new file will be created.", filename.c_str()); "Could not read user config file '%s'. A new file will be created.", filename.c_str());
if(root) delete root;
// Create a default config file - just in case that stk crashes later // Create a default config file - just in case that stk crashes later
// there is a config file that can be modified (to e.g. disable // there is a config file that can be modified (to e.g. disable
// shaders) // shaders)
@ -699,7 +698,6 @@ bool UserConfig::loadConfig()
GUIEngine::showMessage(_("Your config file was too old, so it was deleted and a new one will be created."), 10.0f); GUIEngine::showMessage(_("Your config file was too old, so it was deleted and a new one will be created."), 10.0f);
Log::info("UserConfig", "Your config file was too old, so it was deleted and a new one will be created."); Log::info("UserConfig", "Your config file was too old, so it was deleted and a new one will be created.");
delete root;
return false; return false;
} // if configFileVersion<SUPPORTED_CONFIG_VERSION } // if configFileVersion<SUPPORTED_CONFIG_VERSION
@ -708,7 +706,7 @@ bool UserConfig::loadConfig()
// you want them automatically read from the config file) // you want them automatically read from the config file)
for (unsigned i = 0; i < all_params.size(); i++) for (unsigned i = 0; i < all_params.size(); i++)
{ {
all_params[i]->findYourDataInAChildOf(root); all_params[i]->findYourDataInAChildOf(root.get());
} }
@ -722,7 +720,6 @@ bool UserConfig::loadConfig()
UserConfigParams::m_saved_grand_prix_list.push_back( UserConfigParams::m_saved_grand_prix_list.push_back(
new SavedGrandPrix( saved_gps[i]) ); new SavedGrandPrix( saved_gps[i]) );
} }
delete root;
return true; return true;
} // loadConfig } // loadConfig

View File

@ -77,7 +77,7 @@ EasterEggHunt::~EasterEggHunt()
*/ */
void EasterEggHunt::readData(const std::string &filename) void EasterEggHunt::readData(const std::string &filename)
{ {
XMLNode *easter = file_manager->createXMLTree(filename); auto easter = std::unique_ptr<XMLNode>(file_manager->createXMLTree(filename));
if(!easter) if(!easter)
return; return;
@ -85,7 +85,6 @@ void EasterEggHunt::readData(const std::string &filename)
{ {
Log::error("[EasterEggHunt]", "Can't load easter egg file '%s' - no EasterEggHunt element.", Log::error("[EasterEggHunt]", "Can't load easter egg file '%s' - no EasterEggHunt element.",
filename.c_str()); filename.c_str());
delete easter;
return; return;
} }
@ -111,7 +110,6 @@ void EasterEggHunt::readData(const std::string &filename)
if(!data) if(!data)
{ {
delete easter;
return; return;
} }
m_number_of_eggs = 0; m_number_of_eggs = 0;
@ -129,8 +127,6 @@ void EasterEggHunt::readData(const std::string &filename)
m_number_of_eggs++; m_number_of_eggs++;
} // for i <num_nodes } // for i <num_nodes
delete easter;
WorldStatus::setClockMode(CLOCK_CHRONO); WorldStatus::setClockMode(CLOCK_CHRONO);
} // readEasterEggInfo } // readEasterEggInfo