HEAP error

I have no idea what is going on here.


I use the following code. It is called. Everything is fine.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
void Camera::setPositioning(D3DXVECTOR3 setPosition, float setRadius, float setTheta, float setHeight)
{
	position = setPosition;
	if(setRadius<radiusMin)
		radius = radiusMin;
	else
		radius = setRadius;
	theta = setTheta;
	if(fabsf(theta) >= 2.0f * D3DX_PI)
		theta = 0.0f;
	height = setHeight;
	if (!dynamic)
		constructViewMatrix();
}


I then also call the following code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
void Camera::addPositioning(D3DXVECTOR3 addPosition, float addRadius, float addTheta, float addHeight)
{
	position += addPosition;
	if(radius+addRadius<radiusMin)
		radius = radiusMin;
	else
		radius += addRadius;
	theta += addTheta;
	if(fabsf(theta) >= 2.0f * D3DX_PI)
	//	theta = 0.0f;
	height += addHeight;
	if (!dynamic)
		constructViewMatrix();
}


Notice the line that is commented out. I could replace with theta += addTheta; or whatever to fill in the if statement.
The important part is, if I uncomment that one line, or even have.
1
2
3
4
	
if(fabsf(theta) >= 2.0f * D3DX_PI)
	theta += addTheta;
theta = 0.0f;

or
1
2
3
	
if(fabsf(theta) >= 2.0f * D3DX_PI)
	theta = 0.0f;

I get,

HEAP: Free Heap block 25d6d30 modified at 25d6e98 after it was freed

:S

I have identified &theta as 0x025d6e98 .
theta is defined as,
1
2
3
4
5
6
7
8
9
10
11
12
private:
	unsigned personality:1;
	unsigned dynamic:1;	

	D3DXVECTOR3 position;
	float radius;
	float radiusMin;
	float theta;
	float height;

	D3DXMATRIX view;
	D3DXMATRIX projection;


I can't assign anything to theta in addPositioning(..) without get a HEAP error..

It took me forever to narrow down the error this far.
I can settle for
1
2
3
4
	if(theta >= 2.0f * D3DX_PI)
		theta -= 2.0f * D3DX_PI;
	if(theta <= -2.0f * D3DX_PI)
		theta += 2.0f * D3DX_PI;


However.. WHAT THE HELL?! What is wrong with this thing?
Is your system multi-threaded?

1
2
unsigned personality:1;
	unsigned dynamic:1;


What is the definition for these?
Nope.

Those are flags for my class Camera.
Is the camera class and implementation too long to post?
The reason I am asking is that I cannot see how [code]unsigned personality:1;/code] can be valid C++ code. My compiler throws an error regarding this, as I would expect.

As for your error. At a guess you have allocated some memory, then freed it, and are now trying to use it. The resulting overflow is stuffing up your theta variable. So it does seem like a runtime error caused by an abandoned pointer.


Camera.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
class Camera
{
private:
	unsigned personality:1;		///< 1st or 3rd person, 0 | 1 respectively.
	unsigned dynamic:1;			///< Flag if camera gets updated a lot before uses.

	D3DXVECTOR3 position;	///< 1st: Camera.	3rd: Target.
	float radius;			///< Crow Distance between camera and target.
	float radiusMin;		///< The minimum the radius can be.
	float theta;			///< 1st: Horizontal rotation of target.	3rd: Horizontal rotation of camera.
	float height;			///< 1st: Veritcal rotation of target.		3rd: Height of camera from target.

	D3DXMATRIX view;
	D3DXMATRIX projection;

public:
	Camera(bool setDynamic);
	Camera(bool setDynamic, bool setPersonality);
	Camera(bool setDynamic, bool setPersonality, D3DXVECTOR3 setPosition, float setRadius, float setRadiusMin, float setTheta, float setHeight);
	~Camera();
	void addPositioning(D3DXVECTOR3 addPosition, float addRadius, float addTheta, float addHeight);
	void setPositioning(D3DXVECTOR3 setPosition, float setRadius, float setTheta, float setHeight);
	void constructProjectionMatrix(unsigned int width, unsigned int height);
	void constructViewMatrix();
	D3DXMATRIX* getView();
	D3DXMATRIX* getProjection();

};


Camera.cpp
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
#include "Camera.h"			// For Camera declarations.

Camera::Camera(bool setDynamic)
:dynamic(setDynamic),personality(0),position(0.0f, 0.0f, 0.0f),radius(0),theta(0),height(0){}

Camera::Camera(bool setDynamic, bool setPersonality)
:dynamic(setDynamic),personality(setPersonality),position(0.0f, 0.0f, 0.0f),radius(0),theta(0),height(0){}

Camera::Camera(bool setDynamic, bool setPersonality,D3DXVECTOR3 setPosition,float setRadius, float setRadiusMin, float setTheta, float setHeight)
:dynamic(setDynamic),personality(setPersonality)
{
	radiusMin = setRadiusMin;
	setPositioning(setPosition,setRadius,setTheta,setHeight);
}

Camera::~Camera()
{

}

void Camera::addPositioning(D3DXVECTOR3 addPosition, float addRadius, float addTheta, float addHeight)
{
	position += addPosition;
	if(radius+addRadius<radiusMin)
		radius = radiusMin;
	else
		radius += addRadius;
	theta += addTheta;
	/*
	if(theta >= 2.0f * D3DX_PI)
		theta -= 2.0f * D3DX_PI;
	if(theta <= -2.0f * D3DX_PI)
		theta += 2.0f * D3DX_PI;
	*/
	// Originally,		(Heap error)
	if(fabsf(theta) >= 2.0f * D3DX_PI)
		theta = 0.0f;
	
	height += addHeight;
	if (!dynamic)
		constructViewMatrix();
}

void Camera::setPositioning(D3DXVECTOR3 setPosition, float setRadius, float setTheta, float setHeight)
{
	position = setPosition;
	if(setRadius<radiusMin)
		radius = radiusMin;
	else
		radius = setRadius;
	theta = setTheta;
	if(fabsf(theta) >= 2.0f * D3DX_PI)
		theta -= 2.0f * D3DX_PI;
	if(theta <= -2.0f * D3DX_PI)
		theta += 2.0f * D3DX_PI;
	/*
	if(theta >= 2.0f * D3DX_PI)
		theta -= 2.0f * D3DX_PI;
	if(theta <= -2.0f * D3DX_PI)
		theta += 2.0f * D3DX_PI;
	*/
	// Originally,		(Still worked though)
	if(fabsf(theta) >= 2.0f * D3DX_PI)
		theta = 0.0f;
	
	height = setHeight;
	if (!dynamic)
		constructViewMatrix();
}

void Camera::constructProjectionMatrix(unsigned int width, unsigned int height)
{
	D3DXMatrixPerspectiveFovLH(&projection, D3DX_PI * 0.25f, (float) width / (float) height, 1.0f, 5000.0f);
}

void Camera::constructViewMatrix()
{
	float x = radius * cosf(theta);
	float z = radius * sinf(theta);
	D3DXVECTOR3 pos(x, height, z);
	D3DXVECTOR3 up(0.0f, 1.0f, 0.0f);
	D3DXMatrixLookAtLH(&view,&pos, &position, &up);
}

D3DXMATRIX* Camera::getView()
{
	if (dynamic)
		constructViewMatrix();
	return &view;
}

D3DXMATRIX* Camera::getProjection()
{
	return &projection;
}


CameraController.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class CameraController : public Singleton<CameraController>
{
private:
public:
	void initialise();
	~CameraController();
	Camera* createCamera(bool setDynamic);
	Camera* createCamera(bool setDynamic, bool setPersonality);
	Camera* createCamera(bool setDynamic, bool setPersonality,D3DXVECTOR3 setPosition,float setRadius, float setRadiusMin, float setTheta, float setHeight);
	void releaseCamera(Camera* camera);
	void cameraOnReset(Camera* camera,D3DPRESENT_PARAMETERS d3dPP);
	D3DXMATRIX* getCameraView(Camera* camera);
	D3DXMATRIX* getCameraProjection(Camera* camera);
	void CameraController::addPositioning(Camera* camera, D3DXVECTOR3 addPosition, float addRadius, float addTheta, float addHeight);
	void CameraController::setPositioning(Camera* camera, D3DXVECTOR3 setPosition, float setRadius, float setTheta, float setHeight);
};


CameraController.cpp
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
#include "CameraController.h"	// For CameraController declarations.

	// For easier readability.
CameraController* CameraCtrl = &Singleton<CameraController>::Instance();

void CameraController::initialise()
{
}

CameraController::~CameraController()
{
}

Camera* CameraController::createCamera(bool setDynamic)
{
	return new Camera(setDynamic);
}

Camera* CameraController::createCamera(bool setDynamic, bool setPersonality)
{
	return new Camera(setDynamic, setPersonality);
}

Camera* CameraController::createCamera(bool setDynamic, bool setPersonality, D3DXVECTOR3 setPosition, float setRadius, float setRadiusMin, float setTheta, float setHeight)
{
	return new Camera(setDynamic, setPersonality, setPosition, setRadius, setRadiusMin, setTheta, setHeight);
}

void CameraController::releaseCamera(Camera* camera)
{
	delete camera;
	camera = NULL;
}

void CameraController::cameraOnReset(Camera* camera,D3DPRESENT_PARAMETERS d3dPP)
{
	camera->constructProjectionMatrix(d3dPP.BackBufferWidth,d3dPP.BackBufferHeight);
}

void CameraController::addPositioning(Camera* camera,D3DXVECTOR3 addPosition, float addRadius, float addTheta, float addHeight)
{
	camera->addPositioning(addPosition, addRadius, addTheta, addHeight);
}

void CameraController::setPositioning(Camera* camera,D3DXVECTOR3 setPosition, float setRadius, float setTheta, float setHeight)
{
	camera->setPositioning(setPosition, setRadius, setTheta, setHeight);
}

D3DXMATRIX* CameraController::getCameraView(Camera* camera)
{
	return camera->getView();
}

D3DXMATRIX* CameraController::getCameraProjection(Camera* camera)
{
	return camera->getProjection();
}


Caller Init.
1
2
3
	D3DXVECTOR3 temp(0.0f, 0.0f, 0.0f);
	thirdPerson = CameraCtrl->createCamera(0,1,temp, 10.0f, 5.0f, 1.2 * D3DX_PI, 5.0f);
	hud = CameraCtrl->createCamera(0,0,temp, 10,5,0,0);


Caller Uninit.
1
2
	CameraCtrl->releaseCamera(thirdPerson);
	CameraCtrl->releaseCamera(hud);

Caller Update
 
	CameraCtrl->addPositioning(thirdPerson, temp, y, x, 0.0f);


Update without the line in update. There is no problem.
The reason I am asking is that I cannot see how unsigned personality:1; can be valid C++ code. My compiler throws an error regarding this, as I would expect.


These are bit fields, they will only work within a class or struct.

Try,
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
#include <stdlib.h> // for EXIT_SUCCESS
#include <iostream> // for cout and cin

class Athing
{
public:
	unsigned flag1:1;
	unsigned flag2:2;
	unsigned flag3:2;
	unsigned flag4:1;
	unsigned flag5:1;
};

int main()
{
	Athing purple;
	int num = 0;
	purple.flag1 = num++;
	purple.flag2 = num++;
	purple.flag3 = num++;
	purple.flag4 = num++;
	purple.flag5 = num++;
	std::cout << sizeof(purple) << std::endl;
	std::cout << purple.flag1 << std::endl;
	std::cout << purple.flag2 << std::endl;
	std::cout << purple.flag3 << std::endl;
	std::cout << purple.flag4 << std::endl;
	std::cout << purple.flag5 << std::endl;
	system("pause");
	return(EXIT_SUCCESS);
}
Hmmm it appears that using bit fields is a bad idea. I'd use boolean instead.
http://en.wikipedia.org/wiki/Bit_field

I hope your not calling release before your Update?

I don't see any obvious reason for the memory overwriting though.
Last edited on
I hope your not calling release before your Update?


Nope, I have a state class that updates, when the state is deleted or a new state added, the current state is uninitialised and the new state initialised. So once the unitialisation is done the update method doesn't get called again.

Hmmm it appears that using bit fields is a bad idea. I'd use boolean instead.


Very well then.
On another note.
I've changed the bit fields to booleans, and I am still able to recreate the Heap error. So it wasn't independently fault of the bit fields.
From the code you have specified I cannot see any cause of your heap error.
Topic archived. No new replies allowed.