std::vector Crash

I'm trying to write a component class along the lines of an Entity-Component-System arrangement.
However in the public function TriStripData::Append, when it calls the std::vector .push_back in debug mode (MS VC++ 2008 Express) I get a debug assertion failed error popup:

Debug Assertion Failed!

Program: d:\Files\P\100723_\Debug\100723_.exe
File: f:\dd\vctools\crt_bld\self_x86\crt\src\dbgdel.cpp
Line:52

Expression: _BLOCK_TYPE_IS_VALID(pHead->nBlockUse)


Right at the ending brace of my main function. Code here
main.cpp
1
2
3
4
5
6
7
8
#include <iostream>
#include "component.h"
int main()
{
	Component::TriStripData myTriStripData;
	myTriStripData.Append(Component::TriStrip::Create(4, 4, 10));
	return 0;
}//CRASH 


component.h
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
#ifndef COMPONENT_H
#define COMPONENT_H
#include <vector>
namespace Component
{
class TriStrip
{
public:
	static TriStrip Create(const unsigned int& w_, const unsigned int& h_, const unsigned int& scale_);
	int GetY(const unsigned int& x_, const unsigned int& z_) const;
	void operator=(const TriStrip& another_);
	~TriStrip();
private:
	unsigned int Scale_;
	unsigned int W_;
	unsigned int H_;
	int** Y_Array;
	TriStrip(const unsigned int& w_, const unsigned int& h_, const unsigned int& scale_);
};

class TriStripData
{
public:
	const TriStrip& GetAt(const unsigned int index_)const;
	void Append(const TriStrip& append_);
	void Erase(const unsigned int index_);
private:
	std::vector<TriStrip> Data_;
};

inline TriStrip TriStrip::Create(const unsigned int& w_, const unsigned int& h_, const unsigned int& scale_){
	return TriStrip(w_, h_, scale_); }

inline TriStrip::TriStrip(const unsigned int& w_, const unsigned int& h_, const unsigned int& scale_):W_(w_),H_(h_),Scale_(scale_){	
	Y_Array = new int*[w_];
	for (unsigned int i = 0; i < w_; ++i){
		Y_Array[i] = new int[h_];
	}
	for (unsigned int i = 0; i < w_; ++i){
		for (unsigned int j = 0; j < h_; ++j){
			Y_Array[i][j] = 0;
		}
	}
}

inline int TriStrip::GetY(const unsigned int& x_, const unsigned int& z_)const{
	return Y_Array[x_][z_];
}
inline void TriStrip::operator=(const TriStrip& another_){
	W_ = another_.W_;
	H_ = another_.H_;
	Scale_ = another_.Scale_;
	delete []Y_Array;
	Y_Array = another_.Y_Array;
}
inline TriStrip::~TriStrip()
{
	delete []Y_Array;
}
inline const TriStrip& TriStripData::GetAt(const unsigned int index_)const{
	return Data_[index_];
}
inline void TriStripData::Append(const TriStrip& append_){
	Data_.push_back(append_);
}
inline void TriStripData::Erase(const unsigned int index_){
	Data_.erase(Data_.begin()+index_);
}
}
#endif 
You need to implement a proper copy constructor for TriStrip.
Furthermore,

-> Notice that your assignment operator isn't implemented properly. You should reserve new memory and then copy the actual data, not the pointer. The way you do it now may result in the same memory location being delete[]-ed more than once (by your destructor).

-> Your destructor causes memory leaks. Before delete[]-ing the array you should use a loop (like you do in your constructor) to delete its individual elements.

You can avoid all of this if you implement your 2D array as a vector<vector<int> >
Topic archived. No new replies allowed.