Sign in to follow this  
eSCHEn

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

Recommended Posts

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]

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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].

Share this post


Link to post
Share on other sites
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];

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this