I am in a C++ class and have been assigned a program using composition and dynamic array usage.
I have everything working as it should until the very end before the program closes down. Then I get an Assertion Error
Expression_BLOCK_TYPE_IS_VALID(pHead->nBlockUse)
I have done some research both here and on other web resources. I *know* that I have some sort of array/ pointer issue going on, but I cannot track it down beyond the fact that I think I am deleting them incorrectly. If I comment out the delete lines in my deconstructors, the error does not pop up, but I realize this just hides the problem, and does not handle it.
I am on a windows 7 laptop running Visual C++ express 2010.
These are what I think are the pertinent snippets of code.
This is where I (think) I'm creating the pointer to the array in the .h file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
...
#include "StringTokenizer.h"
#include "CountyList.h"
#include "Alert.h"
using namespace std;
const int Max_Alerts = 200;
class AlertList
{
private:
int numAlerts; //will hold total number of current alerts
Alert *myAlerts; //designates point to array of Alerts
...
| |
and my constructor in the .cpp file creates the array.
1 2 3 4 5
|
AlertList::AlertList(CountyList list)
{
myAlerts = new Alert[Max_Alerts];//creates dynamic array of alerts.
string workingString; //each line of the file
| |
And my deconstructor
1 2 3 4
|
AlertList::~AlertList()
{
delete []myAlerts;
}
| |
Now, I did find some information in the archives that suggseted this:
1 2 3 4 5 6 7
|
AlertList::~AlertList()
{
for (int i = 0; i < numAlerts; i++)
delete [] myAlerts[i];
delete []myAlerts;
}
| |
But I get an error on myAlerts, so I think I am not implementing it correctly.
I think I may be getting very confused on pointers, and handling so many files at once. I have the above situation happening twice in my program and both generate the same error.
My instructor said I could turn it in, but I prefer not to have this sort of error floating around in my programs. Any help would be appreciated.