Advertisement

Error in a class destructor

Started by May 26, 2001 04:06 AM
1 comment, last by TeTE 23 years, 8 months ago
I have the following code:
  
template <unsigned int size>
struct Vector {
	float v[size];

	Vector() {}
	~Vector() {}

	Vector<size> &operator=(const Vector<size> &w);
};

template <unsigned int size>
inline Vector<size> &Vector<size>::operator=(const Vector<size> &w) {
	unsigned int i;

	for (i = 0; i < size; i++)
		v[i] = w.v[i];

	return *this;
}
  
and
  
class OBJModel {
	~OBJModel() {
		...
		delete[] vertices;
	}

	initOBJModel() {
		...
		vertices = new Vector<3>[vertexCount];
		if (!vertices) { _ERROR_ }
	}

private:
	...
	Vector<3> *vertices;
}
  
When ~OBJModel is (automatically) called, produces an error (a memory error, I suppose) with the following message: Debug Assertion Failed! Expression: _BLOCK_TYPE_IS_VALID(pHead->nBlockUse) And the call stack is: _free_dbg(void * 0x007f1db0, int 1) line 1050 + 82 bytes free(void * 0x007f1db0) line 956 + 11 bytes operator delete(void * 0x007f1db0) line 7 + 9 bytes Material::`vector deleting destructor''(unsigned int 3) + 87 bytes Model::~Model() line 17 + 30 bytes I suppose that I''m doing something wrong, but I can''t find the error. If with this info someone could tell me something ...
Hrm...I don''t see a constructor in ObjModel. Are you absolutely sure you''re calling initOBJModel() before the destructor is called? Because if there is no constructor that initializes verticies to null, then verticies will have some random value and trying to delete it will cause something bad to happen.
Advertisement
I haven''t put the complete class. The method initOBJModel reserves all memory and is called in load(), wich loads the file.

You declare an OBJModel and then call load(filename). load() calls initOBJModel and then reads all the data. At the end it calls free() wich frees all the memory. The class doesn''t has a destructor, but I used it here to simplify the post.

And yes, there is constructor wich initializes the pointers to NULL.

This topic is closed to new replies.

Advertisement