back again -.- sorry haha (problem with array deletion)

OK, this is a different project this time, and it isnt like directx based or anything, its just plain c++, i have a class that stores a smallish array of vertices (just 2D points in my own structure format containing an x and y value) to make up a 2D shape, the problem isnt in creating it or anything, the program works somewhat normally, but then when i end the program and the destructor is called, deleting my array, theres an Access violation reading location error, here is my code:
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
protected:
	int numVertices;
	Point2D* shape;
	Point2D* buffer;
	bool closed;

	Point2D position;

public:
	Body()
	{
		setShapeSquare();
	}

	~Body()
	{
		delete [] shape;
	}

	void setShapeSquare()
	{
		numVertices = 4;
		shape = new Point2D[numVertices];
		shape[0].x = 0.0F;	shape[0].y = 0.0F;
		shape[1].x = 10.0F;	shape[1].y = 0.0F;
		shape[2].x = 10.0F;	shape[2].y = 10.0F;
		shape[3].x = 0.0F;	shape[3].y = 10.0F;
		closed = true;
	}


there are other functions but they are just gets and sets, no errors listed, the only problem is when delete is called :/

sorry that i again call upon you c++ gods haha, and thank you for not nagging me about the amount of failures i have :(
Last edited on
if its of any more use, when it breaks, my debugger points to this line

_ASSERTE(_BLOCK_TYPE_IS_VALID(pHead->nBlockUse));

no idea what it does honestly but its in dbgdel.cpp which i assume is a visual studio thing or a c++ standard library thing
If you are copying/assigning objects to each other, they will all try to delete the same buffer.

Say for example:

1
2
3
4
5
6
{
  Body a;  // this allocates 'shape'
  {
    Body b = a;  // this points to a's 'shape'
  } // b'd dtor deletes a.shape
} // a.shape deleted again -- program explode! 


To prevent this, whenever you are managing memory in a class you need to implement the copy constructor and assignment operator as well.
ok ive looked through and i havent copied objects, body is the parent class of particleModel though, and particle models constructors are like so

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
ParticleModel()
		:Body()
	{
		position = Point2D(0.0f, 0.0f);
		velocity = Point2D(0.0f, 0.0f);
		acceleration = 0.0f;
	}

	ParticleModel(Point2D pos)
		:Body()
	{
		position = pos;
		velocity = Point2D(0.0f, 0.0f);
		acceleration = 0.0f;
	}

	ParticleModel(Point2D pos, float acc)
		:Body()
	{
		position = pos;
		velocity = Point2D(0.0f, 0.0f);
		acceleration = acc;
	}


not entirely sure about the :Body() thing, i put that there because body is supposed to be abstract and so i can only initialise particlemodel or any other children of body i make in the future

but yeah, i only initialise shape to be an array in body in the setShapeSquare funtion :/
ok ive looked through and i havent copied objects


Well you should still fix that, regardless. It's possible you are copying without realizing it (returning from a function, putting in a vector or other container, passing to a function, etc would all make a copy)

not entirely sure about the :Body() thing,


It is redundant and unnecessary, but it doesn't hurt anything to have it there.



But yeah. Write an assignment operator and copy constructor for Body, then see if the problem goes away.

OR... instead of doing that, make Body uncopyable and see if you get compiler errors. You can do that by making the copy ctor and assignment private and don't give them any body:

1
2
3
4
5
6
7
8
class Body
{
private:
  Body(const Body&);  // keep them private, don't give them a body
  Body& operator = (const Body&);

//...
};
ok im not fully sure what you mean but i want it so there can be multiple bodies, for polymorphism and stuff, and i dont fully know the purpose of this copy constructor but i do know that so far i havent had to use one, ive made programs like this before but never had this error so i dont know why its happening now, sure i can try to do that copy constructor thing but like i said, i dont know what its for, im still pretty new to c++, enough experience to know the general things though
i managed to solve the problem, turns out the last bit of code that i left in of my tutors skeleton program was the problem, not my code at all haha, turns out he ran a cleanup function and the destructors also ran the function and so it tried deleting twice :/, thanks for the help though
Topic archived. No new replies allowed.