CrtIsValidHeapPointer(pUserData) Error

Hi all,

I am currently trying to teach myself about classes, constructors and destructors. I am writing a program that uses a dynamic array as a vector and trying to implement some of the predefined vector functions. However, when the program ends in main, the error I get is CrtIsValidHeapPointer(pUserData)and it always occurs with my destructor. I have not seen this error before, nor do I know where or how to start looking to correct it. Any and all help is greatly appreciated.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#include <cstdlib>
#include <iostream>

using namespace std;

class VectorDouble
{
	public:
		//Constructors
		VectorDouble();
		VectorDouble(int);

		//Copy Constructor
		VectorDouble(const VectorDouble &sourceObject);

		//Destructor
		~VectorDouble();

		//Overloaded Operators
		void operator = (const VectorDouble &sourceObject);
		friend bool operator == (const VectorDouble &lhs, const VectorDouble &rhs);

		//Member Functions
		void push_back(double);
		int capacity() const;
		int size() const;
		void reserve(int);
		void resize(int);
		void output();

	private:
		double *list;
		int maxCount; //Capacity of vector
		int count; //Size of vector
};

int main()
{
	char ch;
	VectorDouble v1;
	
	v1.push_back(1);
	v1.push_back(2);
	v1.push_back(3);
	v1.push_back(4);
	v1.output();

	cin.get(ch);
	return 0;
}

//Constructors
//===============================================================
VectorDouble::VectorDouble()
{
	list = new double[3];
	maxCount = 3;
	count = 0;
}

VectorDouble::VectorDouble(int initialCapacity)
{
	if (initialCapacity < 0)
	{
		char ch;

		cout << "Illegal value for initial vector capacity. Aborting program.\n";
		cin.get(ch);
		exit(1);
	}

	list = new double[initialCapacity];
	maxCount = initialCapacity;
	count = 0;
}

//Copy Constructor
//===============================================================
VectorDouble::VectorDouble(const VectorDouble &sourceObject)
{
	list = new double[sourceObject.capacity()];
	maxCount = sourceObject.maxCount;
	count = sourceObject.count;
}

//Destructor
//===============================================================
VectorDouble::~VectorDouble()
{
	delete[] list;
	list = NULL;
}

//Overloaded Operators
//===============================================================
void VectorDouble::operator = (const VectorDouble &sourceObject)
{
	list = sourceObject.list;
	maxCount = sourceObject.maxCount;
	count = sourceObject.count;
}

bool operator == (const VectorDouble &lhs, const VectorDouble &rhs)
{
	if (lhs.count == rhs.count)
	{
		for (unsigned int i = 0; i < lhs.count; i++)
		{
			if (lhs.list[i] != rhs.list[i])
				return false;
		}

		return true;
	}

	return false;
}

//Member Functions
//===============================================================
void VectorDouble::push_back(double num)
{
	if (count == maxCount)
	{
		VectorDouble temp(capacity() * 2);

		for (unsigned int i = 0; i < size(); i++)
		{
			temp.list[i] = list[i];
			temp.count++;
		}

		temp.list[count] = num;
		temp.count++;

		maxCount = capacity() * 2;
		count = temp.count;

		for (int i = 0; i < size(); i++)
			list[i] = temp.list[i];
		
	}
	else
	{
		list[count] = num;
		count++;
	}
}

int VectorDouble::capacity() const
{
	return maxCount;
}

int VectorDouble::size() const
{
	return count;
}

void VectorDouble::reserve(int newCapacity)
{
	//TO DO
}

void VectorDouble::resize(int newSize)
{
	//TO DO
}

void VectorDouble::output()
{
	cout << "Vector capacity: " << capacity() << endl;
	cout << "Vector size: " << size() << endl;
	cout << "Vector elements:\n";

	for (unsigned int i = 0; i < count; i++)
		cout << list[i] << endl;
	cout << endl;
}
The code works for me but, you don't need to assign the pointer to NULL in the destructor.

1
2
3
4
5
VectorDouble::~VectorDouble()
{
	delete[] list;

}
Last edited on
Thank you for the speedy response. It also worked the first few times as well, so I started "pushing back" numbers to test it more and started getting the error. Im not sure if it makes any difference but Im using Microsoft Visual Studio Express 2013 for Windows Desktop. I made another test of it and once it told me about a breakpoint, I clicked on the "step over" button and a new window pops up saying that no symbols loaded? Here is some of what I am getting:

C:\Users\Michael\Documents\Programming\C++\Problem Solving with C++ Eighth Edition\Programming Projects\Chapter 11 Friends- Classes\Programming Project 3\Debug\wntdll.pdb: Cannot find or open the PDB file.
C:\Windows\SysWOW64\wntdll.pdb: Cannot find or open the PDB file.
C:\Windows\wntdll.pdb: Cannot find or open the PDB file.
C:\Windows\symbols\dll\wntdll.pdb: Cannot find or open the PDB file.
C:\Windows\dll\wntdll.pdb: Cannot find or open the PDB file.

wntdll.pdb not loaded.

Again, thank you for your help.
Topic archived. No new replies allowed.