fix crash due to a dangling pointer

This commit is contained in:
konstin 2014-05-31 17:28:20 +02:00
parent 6bf1a33ddc
commit 6521e3d496
4 changed files with 38 additions and 20 deletions

View File

@ -27,10 +27,30 @@
#include <set> #include <set>
#include <sstream> #include <sstream>
#include <iostream> // remove later
GrandPrixManager *grand_prix_manager = NULL; GrandPrixManager *grand_prix_manager = NULL;
const char* GrandPrixManager::SUFFIX = ".grandprix"; const char* GrandPrixManager::SUFFIX = ".grandprix";
// ----------------------------------------------------------------------------
GrandPrixManager::GrandPrixManager()
{
m_random_gp = NULL; // better do it explicitly and avoid weird stuff
loadFiles();
}
// ----------------------------------------------------------------------------
GrandPrixManager::~GrandPrixManager()
{
for(unsigned int i=0; i<m_gp_data.size(); i++)
{
delete m_gp_data[i];
}
delete m_random_gp;
}
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
void GrandPrixManager::loadFiles() void GrandPrixManager::loadFiles()
{ {
@ -130,21 +150,6 @@ bool GrandPrixManager::existsName(const irr::core::stringw& name) const
return false; return false;
} }
// ----------------------------------------------------------------------------
GrandPrixManager::GrandPrixManager()
{
loadFiles();
}
// ----------------------------------------------------------------------------
GrandPrixManager::~GrandPrixManager()
{
for(unsigned int i=0; i<m_gp_data.size(); i++)
{
delete m_gp_data[i];
}
}
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
GrandPrixData* GrandPrixManager::getGrandPrix(const std::string& s) const GrandPrixData* GrandPrixManager::getGrandPrix(const std::string& s) const
{ {
@ -154,6 +159,10 @@ GrandPrixData* GrandPrixManager::getGrandPrix(const std::string& s) const
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
GrandPrixData* GrandPrixManager::editGrandPrix(const std::string& s) const GrandPrixData* GrandPrixManager::editGrandPrix(const std::string& s) const
{ {
std::cout << s << std::endl;
if (s == "random")
return m_random_gp;
for(unsigned int i=0; i<m_gp_data.size(); i++) for(unsigned int i=0; i<m_gp_data.size(); i++)
{ {
if(m_gp_data[i]->getId() == s) if(m_gp_data[i]->getId() == s)

View File

@ -47,6 +47,10 @@ private:
bool existsName(const irr::core::stringw& name) const; bool existsName(const irr::core::stringw& name) const;
public: public:
/** saved here by a random GP dialog to avoid dangling pinters or
* memory leaks */
GrandPrixData* m_random_gp;
GrandPrixManager(); GrandPrixManager();
~GrandPrixManager(); ~GrandPrixManager();
void reload(); void reload();

View File

@ -45,7 +45,14 @@ RandomGPInfoDialog::RandomGPInfoDialog()
m_over_body = m_area.getHeight()/7 + SPINNER_HEIGHT + 10; // 10px space m_over_body = m_area.getHeight()/7 + SPINNER_HEIGHT + 10; // 10px space
m_lower_bound = m_area.getHeight()*6/7; m_lower_bound = m_area.getHeight()*6/7;
// The GP manager is be used to make the GP live longer than this dialog
if (grand_prix_manager->m_random_gp)
{
delete grand_prix_manager->m_random_gp;
grand_prix_manager->m_random_gp = NULL;
}
m_gp = new GrandPrixData(m_number_of_tracks, m_trackgroup, m_use_reverse); m_gp = new GrandPrixData(m_number_of_tracks, m_trackgroup, m_use_reverse);
grand_prix_manager->m_random_gp = m_gp;
addTitle(); addTitle();
addSpinners(); addSpinners();
@ -91,9 +98,8 @@ GUIEngine::EventPropagation RandomGPInfoDialog::processEvent(
if (eventSource == "start") if (eventSource == "start")
{ {
// Save the gp since dismiss deletes it otherwise // Save the gp since dismiss deletes it otherwise
GrandPrixData buff = *m_gp;
ModalDialog::dismiss(); ModalDialog::dismiss();
race_manager->startGP(&buff, false, false); race_manager->startGP(m_gp, false, false);
return GUIEngine::EVENT_BLOCK; return GUIEngine::EVENT_BLOCK;
} }
else if (eventSource == "Number of tracks") else if (eventSource == "Number of tracks")
@ -132,10 +138,10 @@ GUIEngine::EventPropagation RandomGPInfoDialog::processEvent(
void RandomGPInfoDialog::updateGP() void RandomGPInfoDialog::updateGP()
{ {
if (m_gp != NULL) delete m_gp;
delete m_gp;
m_gp = new GrandPrixData(m_number_of_tracks, m_trackgroup, m_use_reverse); m_gp = new GrandPrixData(m_number_of_tracks, m_trackgroup, m_use_reverse);
grand_prix_manager->m_random_gp = m_gp;
addTracks(); addTracks();
} }

View File

@ -33,7 +33,6 @@ public:
static const int SPINNER_HEIGHT = 40; static const int SPINNER_HEIGHT = 40;
RandomGPInfoDialog(); RandomGPInfoDialog();
~RandomGPInfoDialog() { delete m_gp; }
/** Adds a SpinnerWidgets to choose the track groups and one to choose the /** Adds a SpinnerWidgets to choose the track groups and one to choose the
* number of tracks */ * number of tracks */