CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 13 of 13
  1. #1
    Guest

    deleting array with delete causes an exception...

    When I try to delete an array of a class consisting of two char pointers (strings) I get an exception. This does not happen when i declare the strings a given size (like char str1[16] ) but when I declare them as pointer and create them with new I get the exception.

    they aren't deleted twice because if I don't delete them I get a memory leek... so is there a way to delete the array without an exception.
    ( I've tried delete and delete[] with no sucess )

    //Calle Lennartsson


  2. #2
    Join Date
    Apr 1999
    Location
    Toronto, ON
    Posts
    713

    Re: deleting array with delete causes an exception...

    Show your code.


    Dmitriy, MCSE

  3. #3
    Guest

    Re: deleting array with delete causes an exception...

    ok...

    class m_Class {
    public:
    char *APUID;
    char *ID;
    double SenasteDatum;
    };

    then I declare it in another class.

    m_Class *m_Data;

    m_Data = new m_Class[10];

    then I create the strings with:
    ID = new char[20];
    strcpy(ID, "Testing...");
    and so on...

    then when I delete the m_Data with

    delete m_Data;

    I recieve the exception..

    if this code isn't enough let me know..

    Thanks in advance.. Calle





  4. #4
    Join Date
    May 1999
    Location
    CA, USA
    Posts
    586

    Re: deleting array with delete causes an exception...

    class m_Class {
    public:
    char *APUID;
    char *ID;
    double SenasteDatum;
    };

    then I declare it in another class.

    m_Class *m_Data;

    m_Data = new m_Class[10];

    then I create the strings with:

    ID = new char[20];
    strcpy(ID, "Testing...");

    Now - in the m_Class constructor and destructor:

    m_Class::m_Class()
    {
    APUID = NULL;
    ID = NULL;
    }

    m_Class::~m_Class
    {
    if (APUID)
    delete []APUID;

    if (ID)
    delete []ID;
    }

    I would also add the following check when you allocate the memory for APUID and ID

    if (APUID)
    delete []APUID;

    APUID = new char[iValue];

    and:

    if (ID)
    delete []ID;

    ID = new char[iValue];

    You're only deleting the memory allocated for the class, but not the memory allocated for the char arrays.

    Rail

    ------------
    Recording Engineer/Software Developer
    Rail Jon Rogut Software
    http://home.earthlink.net/~railro/
    [email protected]

  5. #5
    Guest

    Re: deleting array with delete causes an exception...

    Thanks.. solved my problem

    //Calle Lennartsson


  6. #6
    Join Date
    Aug 1999
    Posts
    6

    Re: deleting array with delete causes an exception...

    Oops.. seems I was a bit too fast in my "everyting is fine" respone.

    I still get the error but now I've made a better example of what I mean.

    In Temp.h

    class CTemp
    {
    public:
    class m_Class {
    public:
    void Fill();
    m_Class();
    virtual ~m_Class();
    char *sStr;
    int iTemp;
    };
    void Remove();
    void Create();
    m_Class *m_Data;
    CTemp();
    virtual ~CTemp();
    };




    And in Temp.cpp

    CTemp::m_Class::m_Class()
    {
    iTemp = 0;
    sStr = NULL;
    }

    CTemp::m_Class::~m_Class()
    {
    if (sStr)
    delete []sStr;
    }

    CTemp::CTemp()
    {
    m_Data = NULL;
    }

    CTemp::~CTemp()
    {
    if (m_Data)
    Remove();
    }

    void CTemp::Create()
    {
    m_Data = new m_Class[1];
    m_Data[1].Fill();
    }

    void CTemp::Remove()
    {
    if (m_Data)
    delete m_Data;
    }

    void CTemp::m_Class::Fill()
    {
    iTemp = 15;
    sStr = new char[15];
    strcpy(sStr,"Testing hmm");
    }




    Then in my main program I do:

    CTemp Temp;
    Temp.Create();
    Temp.Remove();




    I then recieve the exception in the Temp.Remove() call to delete m_Data.
    What is wrong here?

    //Calle Lennartsson


  7. #7
    Join Date
    Apr 1999
    Posts
    27,449

    Re: deleting array with delete causes an exception...

    Your problem is the following:

    void CTemp::Create()
    {
    m_Data = new m_Class[1];
    m_Data[1].Fill(); <<-- This should be m_Data[0]!!!
    }




    Arrays are 0 based. By specifying m_Data[1], you are writing to an invalid address. Also, why is the destructor in the nested class "m_Class" virtual? You can't derive from it, so there is no need for a v-table to be built from a nested class.



  8. #8
    Join Date
    Jul 1999
    Posts
    15

    Re: deleting array with delete causes an exception...

    Another problem in your code Which I Think is the main reason why you have all these problems is that you are calling Delete twice. In your main function remoive the call
    Temp.Remove(); and you should be all set. This is because your destructor is calling the Remove hence you do not need to do anything.
    Wyne


  9. #9
    Join Date
    Apr 1999
    Location
    Toronto, ON
    Posts
    713

    Re: deleting array with delete causes an exception...

    It is good. But you do not have to write so.
    Do not use pointer to your child class.
    Just define it so:
    class CTemp
    {
    public:
    class m_Class {
    public:
    void Fill();
    m_Class();
    virtual ~m_Class();
    char *sStr;
    int iTemp;
    }m_yourChild ;
    void Remove();
    void Create();
    CTemp();
    virtual ~CTemp();
    };
    now you have easy access to your child:
    CTemp::m_YourChild - this is your m_Class and you do not need to delete this class.

    Because delete works in way like malloc - it has linked list of memory blocks that is in use. Info about this block are in head of the block that has address (I do not remember exactly!) something like variable-30 bytes.
    For example: your class m_Class allocated at 0x12060. When you try to delete this, operator delete read info about this allocated memory block from address 0x12030. But you have another data there. It produce crush. Never use "delete" with no "new"!

    Dmitriy, MCSE

  10. #10
    Join Date
    Aug 1999
    Posts
    6

    Re: deleting array with delete causes an exception...

    Ok that was a typo, sorry.

    What I want to do is to create an array of the member class like so:

    int mMax = 15;
    m_Data = new m_Class[mMax];
    m_Data[3].Fill();
    m_Data[9].Fill();



    ... and so on.


  11. #11
    Join Date
    Aug 1999
    Posts
    6

    Re: deleting array with delete causes an exception...

    But the exception is caught in the FIRST call to delete. I understand that I should have set m_Data = NULL in the Remove() funktion.
    I still want the Remove() funktion to work bescause I might want to do a

    Temp.Fill();
    Temp.Remove();
    Temp.Fill(); //Again





  12. #12
    Join Date
    Aug 1999
    Posts
    6

    Re: deleting array with delete causes an exception...

    The only problem with that is that I want to have an array of the child class in my Class.
    See also my response to Paul McKenzie.


  13. #13
    Join Date
    Jul 1999
    Posts
    18

    Re: deleting array with delete causes an exception...

    Hello Calle,

    Inside your Remove function :

    void CTemp::Remove()
    {
    // Change this
    //if (m_Data)
    // delete m_Data;
    if ( m_Data )
    {
    delete [] m_Data;
    }
    }




    HTH,
    YEOJ


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured