Skip to main content
GameDev.net gamedev.net
🔒 Locked

Errors when deleting dynamic memory stored within std::map [solved]

Started by eSCHEn Mar 25, 2006 at 8:43 PM 4 replies 1.4k views
Original Post
eSCHEn
eSCHEn
Hi, I'm using a std::map to contain some dynamically allocated classes within C++. The classes are constructed in the main loop and then added to a std::map, which is then responsible for them. Everything works hunky-dory until I come to clear out the map, when I get nasty errors galore. Here's a link to the error in question: clicky (9Kb). Here's an example of how I'm declaring and using the std::map:
// definition of map type
typedef std::map<std::string, gui::CForm*> TStringFormMap;

// this is how I add a form
bool CApplication::AddForm(gui::CForm* Form)
{
    // error checking
    if (Form == NULL)
        return false;
    if (Form->GetName() == "")
        return false;
    if (m_forms.find(Form->GetName()) != m_forms.end())
        return false;

    // add form
    m_forms[Form->GetName()] = Form;
    return true;
}

// this is how I delete a form
void CApplication::DeleteForm(const std::string FormName)
{
    if (this->Forms(FormName) != NULL)
    {
        if (FormName == m_focused_form)
            m_focused_form.clear();
        gui::CForm* form_del = m_forms[FormName];
        delete form_del;
        form_del = NULL;
        m_forms.erase(FormName);
    }
}


// sample usage
gui::CForm form1;
g_application.AddForm(&form1);
// blah...
// kill form
g_application.DeleteForm(form1.GetName());  // bang!

The "DeleteForm" line at the bottom is what generates the error in question. Many thanks. [Edited by - eSCHEn on March 25, 2006 9:18:02 PM]
--
Cheers,
Darren Clark
dave
dave
Hey bud,

The first thing to check is that the form name you are deleting actually exists. there is no checking for this in the deleting function.

That's all i can think of i'm afraid, hope that helps,

Dave
eSCHEn
eSCHEn
Hi Dave, thanks for the suggestion but unfortunately I already check for a form's existence using this->Forms("") which returns NULL if the form doesn't exist within the map.

Thanks for trying though [smile].
--
Cheers,
Darren Clark
graveyard filla
graveyard filla
hmmm

it looks like you are creating a Form on the stack, and then trying to delete it....

this is no good!

this:
gui::CForm form1;
g_application.AddForm(&form1);


needs to become this:
gui::CForm *form1 = new gui::CForm();
g_application.AddForm(form1);

you cannot delete memory you didn't allocate yourself.... why are you even using pointers if you are going to create it on the stack?


PS, you can consolidate the following 3 lines:

gui::CForm* form_del = m_forms[FormName];
delete form_del;
form_del = NULL;

into this:

delete m_forms[FormName];
FTA, my 2D futuristic action MMORPG
eSCHEn
eSCHEn
I love you forever man, you just sorted it [grin]. Thanks a lot for the code suggestion as well, always appreciated!

If you're ever in SW Scotland then you're more than welcome to come in for a nice cup of tea and some biscuits :)

Rating++ for you my good sir.
--
Cheers,
Darren Clark

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.