Heap Corruption!?

I have a small program that compiles and runs, but after execution, Visual Studio is giving me the following error message:
Windows has triggered a breakpoint in LAN.exe. This may be due to a corruption of the heap, which indicates a bug in LAN.exe or any of the DLLs it has loaded.

I noticed that if I comment out the second to last line in Network::add then I don't get the error, which suggests that I am assigning the Node to the Node* incorrectly. However, it compiles and outputs correctly to the console. As you can see from Main.cpp, only the Network constructor, Network::add, and the Node constructor are actually called. I would very much appreciate it if anyone can see what the problem is. :D

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
// Main.cpp
#include <iostream>
#include <cstdlib>
#include "Network.h"

using namespace std;

int main() {
	Network network;
	Node node;
	network.add(node);
	cout << "network.size(): " << network.size() << endl;
	network.add(node);
	cout << "network.size(): " << network.size() << endl;
	Node node2;
	network.add(node2);
	cout << "network.size(): " << network.size() << endl;

	system("pause");
	return EXIT_SUCCESS;
}

// Network.h
#ifndef NETWORK_H
#define NETWORK_H

#include "Node.h"

class Network {
public:
	Network(const unsigned = 10);
	bool add(Node&);
	const unsigned size() const;
	~Network();

private:
	void realloc();

	Node** nodes;
	unsigned node_count;
	unsigned capacity;
};
#endif

// Network.cpp
#include "Network.h"

Network::Network(const unsigned init_capacity)
: node_count(0), capacity(init_capacity) {
	nodes = new Node*[capacity];
	for (unsigned i = 0; i < capacity; i++) {
		nodes[i] = 0;
	}
}

Network::~Network() {
	for (unsigned i = 0; i < node_count; i++){
		delete nodes[i];
	}
	delete [] nodes;
}

bool Network::add(Node& node) {
	for (unsigned i = 0; i < node_count; i++) {
		if (nodes[i] == &node) {
			return false;
		}
	}
	if (node_count == capacity) {
		realloc();
	}
	nodes[node_count++] = &node;
	return true;
}

void Network::realloc() {
	Node** new_nodes = new Node*[capacity *= 2];
	unsigned i = 0;
	for (; i < node_count; i++) {
		new_nodes[i] = nodes[i];
	}
	for (; i < capacity; i++) {
		new_nodes[i] = 0;
	}
	delete [] nodes;
	nodes = new_nodes;
}

const unsigned Network::size() const {
	return node_count;
}

// Node.h
#ifndef NODE_H
#define NODE_H

class Node {
public:
	Node();
	~Node();

private:
	Node* nodes;
	const unsigned id;
	static unsigned id_count;
};

#endif

// Node.cpp
#include "Node.h"

unsigned Node::id_count = 0;

Node::Node()
: id(id_count++), nodes(0) {
}

Node::~Node() {
}
Network assumes the Nodes come from the heap, but you're passing one off the stack.
In the Network() destructor, you're trying to delete a stack variable - since you didn't allocate it with new, delete won't work on that address. That's why it says there's a corruption of the heap - it's not on the heap to start with.
Well, that explains it. Originally it was allocated from the heap and I changed it. I guess I forgot about the destructor. Thanks.
Topic archived. No new replies allowed.