|
-
August 22nd, 1999, 09:08 AM
#1
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
-
August 22nd, 1999, 09:37 AM
#2
Re: deleting array with delete causes an exception...
Show your code.
Dmitriy, MCSE
-
August 22nd, 1999, 01:31 PM
#3
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
-
August 22nd, 1999, 02:22 PM
#4
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]
-
August 22nd, 1999, 04:01 PM
#5
Re: deleting array with delete causes an exception...
Thanks.. solved my problem
//Calle Lennartsson
-
August 23rd, 1999, 02:08 PM
#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
-
August 23rd, 1999, 02:19 PM
#7
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.
-
August 23rd, 1999, 02:42 PM
#8
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
-
August 23rd, 1999, 03:18 PM
#9
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
-
August 24th, 1999, 12:24 AM
#10
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.
-
August 24th, 1999, 12:27 AM
#11
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
-
August 24th, 1999, 12:30 AM
#12
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.
-
August 24th, 1999, 04:52 AM
#13
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|