Click to See Complete Forum and Search --> : deleting array with delete causes an exception...
August 22nd, 1999, 09:08 AM
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
Dmitriy
August 22nd, 1999, 09:37 AM
Show your code.
Dmitriy, MCSE
August 22nd, 1999, 01:31 PM
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
Rail Jon Rogut
August 22nd, 1999, 02:22 PM
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;
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
[i]Rail Jon Rogut Software
http://home.earthlink.net/~railro/
railro@earthlink.net
August 22nd, 1999, 04:01 PM
Thanks.. solved my problem
//Calle Lennartsson
calle
August 23rd, 1999, 02:08 PM
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
Paul McKenzie
August 23rd, 1999, 02:19 PM
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.
Wyne
August 23rd, 1999, 02:42 PM
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
Dmitriy
August 23rd, 1999, 03:18 PM
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
calle
August 24th, 1999, 12:24 AM
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.
calle
August 24th, 1999, 12:27 AM
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
calle
August 24th, 1999, 12:30 AM
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.
jdala
August 24th, 1999, 04:52 AM
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
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.