Unhandled exception at 0x010B3FF0 ???

Whenever I run my code I get this error "Unhandled exception at 0x010B3FF0 in Assignment 2 of 2 ASCII.exe: 0xC0000005: Access violation writing location 0x00000000."

In which it points to this code being the error.
1
2
3
4
5
 void ASCIIRenderer::SetPixel(int x, int y, CHAR_INFO pixelData)
{
	m_ScreenBuffer[x + (y*SCREEN_WIDTH)].Char.AsciiChar = pixelData.Char.AsciiChar;
	m_ScreenBuffer[x + (y*SCREEN_WIDTH)].Attributes = pixelData.Attributes;
}
Last edited on
maybe "x + (y*SCREEN_WIDTH)" is greater than "m_ScreenBuffer.size()"
This is what I have with screenbuffer and size if there something that i'm missing:

1
2
const int SCREEN_WIDTH = 80;
const int SCREEN_HEIGHT = 25;


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
void ASCIIRenderer::SetScreenSize()
{
	COORD MaxWindowSize = GetLargestConsoleWindowSize(m_hConsole);
	int targetWidth = min(MaxWindowSize.X, SCREEN_WIDTH);
	int targetHeight = min(MaxWindowSize.Y, SCREEN_HEIGHT);
	SMALL_RECT rect = { 0, 0, targetWidth - 1, targetHeight - 1 };
	COORD coord = { targetWidth, targetHeight };
	bool bufferSizeSet = SetConsoleScreenBufferSize(m_hConsole, coord);
	bool windowInfoSet = SetConsoleWindowInfo(m_hConsole, TRUE, &rect);
}
void ASCIIRenderer::InitialiseScreenBuffer(CHAR asciiChar, WORD attributes)
{
	for (int i = 0; i < SCREEN_WIDTH; i++)
	{
		for (int j = 0; j < SCREEN_HEIGHT; j++)
		{
			m_ScreenBuffer[SCREEN_WIDTH * j + i].Char.AsciiChar = asciiChar;
			m_ScreenBuffer[SCREEN_WIDTH * j + i].Attributes = attributes;
		}
	}
}
Last edited on
Where did you create m_ScreenBuffer?
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
#include <iostream>
#include <windows.h>
using namespace std;

const int SCREEN_WIDTH = 80;
const int SCREEN_HEIGHT = 25;

class ASCIIRenderer
{

private:
	CHAR_INFO m_ScreenBuffer[SCREEN_WIDTH * SCREEN_HEIGHT];
	HANDLE m_hConsole;
	void GetConsoleHandle();
	void SetScreenSize();
	void InitialiseScreenBuffer(CHAR asciiChar = 0, WORD attributes = 15);

public:
	ASCIIRenderer();
	~ASCIIRenderer();

	void Initialise();
	void RenderBufferToConsole();
	void ClearScreen();
	void FillScreen(CHAR asciiChar = 0, WORD attributes = 15);
	void SetPixel(int x, int y, CHAR_INFO pixelData);
};
Maybe you used x/y (swapped) wrongfully. In order to ensure the right usage you can use assert(...) in SetPixel(...):

http://www.cplusplus.com/reference/cassert/assert/?kw=assert

1
2
3
4
5
6
7
8
 void ASCIIRenderer::SetPixel(int x, int y, CHAR_INFO pixelData)
{
assert (x < SCREEN_WIDTH);
assert (y < SCREEN_HEIGHT);

	m_ScreenBuffer[x + (y*SCREEN_WIDTH)].Char.AsciiChar = pixelData.Char.AsciiChar;
	m_ScreenBuffer[x + (y*SCREEN_WIDTH)].Attributes = pixelData.Attributes;
}
no that doesn't seem to be the problem, still getting the unhandled exception error
Try this:
1
2
3
4
5
6
7
8
9
10
11
12
void ASCIIRenderer::SetPixel(int x, int y, CHAR_INFO pixelData)
{
  const int MAX_INDEX = SCREEN_HEIGHT * SCREEN_WIDTH - 1;
  int index = x + (y*SCREEN_WIDTH);

  assert(x >= 0);
  assert(y >= 0);
  assert(index > 0 && index <= MAX_INDEX);
  
  m_ScreenBuffer[x + (y*SCREEN_WIDTH)].Char.AsciiChar = pixelData.Char.AsciiChar;
  m_ScreenBuffer[x + (y*SCREEN_WIDTH)].Attributes = pixelData.Attributes;
}


If this doesn't help post the whole so that we can compile it.
It still crashed but with a different error:

http://prntscr.com/du68hj
assert(index > 0 && index <= MAX_INDEX);

OOPS, my mistake, should be assert(index >= 0 && index <= MAX_INDEX);
If it still fails then you need to check the value of index in the debugger
@Thomas1965

This assert(index > 0 && index <= MAX_INDEX); should be assert(index >= 0 && index <= MAX_INDEX);

In case x and y are both 0.


@MackieJ
Please post relevant code, i.e. where the call to SetPixel(...) are actually happens.
nope still occuring here's my full 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
#include <iostream>
#include "ASCIIRenderer.h"
#include <windows.h>
#include "BouncingBall.h"
using namespace std;

int main()
{
	bool quit = false;
	ASCIIRenderer renderer;
	BouncingBall ball;

	unsigned short int pass = 0;
	while (!quit)
	{
		renderer.Initialise();
		renderer.FillScreen(0, pass++);
		renderer.RenderBufferToConsole();
		Sleep(50);
		ball.Initialise(10, 10, 0, 0, &renderer);
		ball.Render();
		ball.Update();
	}
	return 0;
}

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
#include <iostream>
#include <windows.h>
using namespace std;

const int SCREEN_WIDTH = 80;
const int SCREEN_HEIGHT = 25;

class ASCIIRenderer
{

private:
	CHAR_INFO m_ScreenBuffer[SCREEN_WIDTH * SCREEN_HEIGHT];
	HANDLE m_hConsole;
	void GetConsoleHandle();
	void SetScreenSize();
	void InitialiseScreenBuffer(CHAR asciiChar = 0, WORD attributes = 15);

public:
	ASCIIRenderer();
	~ASCIIRenderer();

	void Initialise();
	void RenderBufferToConsole();
	void ClearScreen();
	void FillScreen(CHAR asciiChar = 0, WORD attributes = 15);
	void SetPixel(int x, int y, CHAR_INFO pixelData);
};

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
#include <iostream>
#include "ASCIIRenderer.h"
#include <windows.h>
#include <assert.h>
using namespace std;

HANDLE m_hConsole;


void ASCIIRenderer::GetConsoleHandle()
{
	m_hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
}
void ASCIIRenderer::SetScreenSize()
{
	COORD MaxWindowSize = GetLargestConsoleWindowSize(m_hConsole);
	int targetWidth = min(MaxWindowSize.X, SCREEN_WIDTH);
	int targetHeight = min(MaxWindowSize.Y, SCREEN_HEIGHT);
	SMALL_RECT rect = { 0, 0, targetWidth - 1, targetHeight - 1 };
	COORD coord = { targetWidth, targetHeight };
	bool bufferSizeSet = SetConsoleScreenBufferSize(m_hConsole, coord);
	bool windowInfoSet = SetConsoleWindowInfo(m_hConsole, TRUE, &rect);
}
void ASCIIRenderer::InitialiseScreenBuffer(CHAR asciiChar, WORD attributes)
{
	for (int i = 0; i < SCREEN_WIDTH; i++)
	{
		for (int j = 0; j < SCREEN_HEIGHT; j++)
		{
			m_ScreenBuffer[SCREEN_WIDTH * j + i].Char.AsciiChar = asciiChar;
			m_ScreenBuffer[SCREEN_WIDTH * j + i].Attributes = attributes;
		}
	}
}
void ASCIIRenderer::Initialise()
{
	GetConsoleHandle();
	SetScreenSize();
	InitialiseScreenBuffer();
	SetConsoleTitle("4107COMP PONGOUT");
}
void ASCIIRenderer::RenderBufferToConsole()
{
	COORD coord = { SCREEN_WIDTH, SCREEN_HEIGHT };
	COORD coord2 = { 0, 0 };
	SMALL_RECT write = { 0, 0, SCREEN_WIDTH - 1, SCREEN_HEIGHT - 1 };
	WriteConsoleOutput(m_hConsole, (const CHAR_INFO *)(&m_ScreenBuffer), coord, coord2, &write);
}
void ASCIIRenderer::ClearScreen()
{
	InitialiseScreenBuffer();
}
void ASCIIRenderer::FillScreen(CHAR asciiChar, WORD attributes)
{
	InitialiseScreenBuffer(asciiChar, attributes);
}
void ASCIIRenderer::SetPixel(int x, int y, CHAR_INFO pixelData)
{
	const int MAX_INDEX = SCREEN_HEIGHT * SCREEN_WIDTH - 1;
	int index = x + (y*SCREEN_WIDTH);

	assert(x >= 0);
	assert(y >= 0);
	assert(index >= 0 && index <= MAX_INDEX);

	m_ScreenBuffer[x + (y*SCREEN_WIDTH)].Char.AsciiChar = pixelData.Char.AsciiChar; //problem occurs when its hits this line
	m_ScreenBuffer[x + (y*SCREEN_WIDTH)].Attributes = pixelData.Attributes;
}

ASCIIRenderer::ASCIIRenderer()
{
	Initialise();
}
ASCIIRenderer::~ASCIIRenderer()
{

}

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
#include <iostream>
#include <windows.h>
#include "Vector2D.h"
using namespace std;

class BouncingBall
{

public:
	BouncingBall();
	~BouncingBall();
	int m_PositionX;
	int m_PositionY;
	int m_DirectionX;
	int m_DirectionY;
	ASCIIRenderer* m_pRenderer;
	int m_PreviousX;
	int m_PreviousY;
	float m_Width;
	float m_Height;
	
	void Initialise(int m_PositionX, int m_PositionY, int m_DirectionX, int m_DirectionYconst, ASCIIRenderer* m_pRenderer);
	void Update();
	void Render();

private:

};

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
#include "ASCIIRenderer.h"
#include "BouncingBall.h"
#include <windows.h>
using namespace std;


void BouncingBall::Initialise(int m_PositionX, int m_PositionY, int m_DirectionX, int m_DirectionY, ASCIIRenderer* m_pRenderer)
{

}
void BouncingBall::Update()
{
	m_PreviousX = m_PositionX;
	m_PreviousY = m_PositionY;

	m_PositionX += m_DirectionX;
	m_PositionY += m_DirectionY;

	if (m_PositionX < 0 || m_PositionX > SCREEN_WIDTH)
	{
		m_PositionX = m_PreviousX;
		m_PositionY = m_PreviousY;
		m_DirectionX = -m_DirectionX;
	}

	if (m_PositionY < 0 || m_PositionY > SCREEN_HEIGHT)
	{
		m_PositionX = m_PreviousX;
		m_PositionY = m_PreviousY;
		m_DirectionY = -m_DirectionY;
	}

};
void BouncingBall::Render()
{
	if (m_pRenderer == NULL)
	{
		CHAR_INFO ball;
		ball.Char.AsciiChar = 0;
		ball.Attributes = BACKGROUND_RED;
		m_pRenderer->SetPixel(m_PositionX, m_PositionY, ball);
	}
}
BouncingBall::BouncingBall()
{
	Initialise(0, 0, 0, 0, 0);
	m_pRenderer = NULL;
}
BouncingBall::~BouncingBall()
{

}
Well
1
2
3
4
5
6
7
8
9
10
void BouncingBall::Render()
{
	if (m_pRenderer == NULL) // This means: Use m_pRenderer when it is invalid -> m_pRenderer != NULL
	{
		CHAR_INFO ball;
		ball.Char.AsciiChar = 0;
		ball.Attributes = BACKGROUND_RED;
		m_pRenderer->SetPixel(m_PositionX, m_PositionY, ball); // Crash starts here
	}
}
You need to create the renderer instead of just set it to NULL

EDIT:
I actually think that you need to pass the renderer to the BouncingBall as the paramter of the constructor
Last edited on
In your SetPixel method x and y are < 0 so you have an invalid index. The actual problem is in BouncingBall::Render.
Yes I had done that prior to having it as != with the same error still
Yes I had done that prior to having it as != with the same error still
No, that's not possible because SetPixel(...) isn't called and hence cannot crash. Except you left m_pRenderer uninitialzed.

if (m_pRenderer == NULL) is wrong whatsoever.
What's the point of calling BouncingBall::Initialise when it doesn't do anything?
1
2
3
4
5
void BouncingBall::Initialise(int m_PositionX, int m_PositionY, int m_DirectionX,
                                                   int m_DirectionY, ASCIIRenderer* m_pRenderer)
{

}
In order to solve your problem I would suggest this:

1. Remove m_pRenderer as membervaribale entirely from the class BouncingBall.
2. Change the Render() function:

1
2
3
4
5
6
7
void BouncingBall::Render(ASCIIRenderer &renderer)
{
	CHAR_INFO ball;
	ball.Char.AsciiChar = 0;
	ball.Attributes = BACKGROUND_RED;
	renderer.SetPixel(m_PositionX, m_PositionY, ball);
}
I managed to get it working by passing the position, direction and renderer into initialise

1
2
3
4
5
6
7
8
void BouncingBall::Initialise(int PositionX,int PositionY,int DirectionX,int DirectionY,ASCIIRenderer* pRenderer)
{
	m_PositionX = PositionX;
	m_PositionY = PositionY;
	m_DirectionX = DirectionX;
	m_DirectionY = DirectionY;
	m_pRenderer = pRenderer;
}

although I still haven't gotten my ball to work
Last edited on
although I still haven't gotten my ball to work
What does that mean?
Topic archived. No new replies allowed.