Day 004 Access Violation Error.

Can't find the error in my code but I know that its there somewhere. Could just be from some VS2015 weirdness. Could someone help me out? Heres 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
 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
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#include <windows.h>
#include <stdint.h>

#define Internal static
#define LocalPersist static
#define GlobalVariable static

typedef uint8_t uint8;
typedef uint16_t uint16;
typedef uint32_t uint32;
typedef uint64_t uint64;

typedef int8_t  int8;
typedef int16_t int16;
typedef int32_t int32;

GlobalVariable bool Running;

GlobalVariable BITMAPINFO BitmapInfo;
GlobalVariable void *BitmapMemory;

GlobalVariable int BitmapWidth;
GlobalVariable int BitmapHeight;

GlobalVariable WNDCLASS WindowClass;

Internal void Win32ResizeDIBSection(int Width, int Height) {

	if (BitmapMemory) {
		VirtualFree(BitmapMemory, 0, MEM_RELEASE);
	}

	BitmapWidth = Width;
	BitmapHeight = Height;

	BitmapInfo.bmiHeader.biSize = sizeof(BitmapInfo.bmiHeader);
	BitmapInfo.bmiHeader.biWidth = BitmapWidth;
	BitmapInfo.bmiHeader.biHeight = -BitmapHeight;
	BitmapInfo.bmiHeader.biPlanes = 1;
	BitmapInfo.bmiHeader.biBitCount = 32;
	BitmapInfo.bmiHeader.biCompression = BI_RGB;

	int BytesPerPixel = 4;
	int BitmapMemorySize = (BitmapWidth, BitmapHeight) * BytesPerPixel;
	BitmapMemory = VirtualAlloc(0, BitmapMemorySize, MEM_COMMIT, PAGE_READWRITE);

	int Pitch = Width * BytesPerPixel;
	uint8 *Row = (uint8 *)&BitmapMemory;
	for (int Y = 0; Y < BitmapHeight; ++Y) {
		uint32 *Pixel = (uint32 *)&Row;
		for (int X = 0; X < BitmapWidth; ++X) {

			*Pixel = 255;
			++Pixel;

			*Pixel = 0;
			++Pixel;

			*Pixel = 0;
			++Pixel;

			*Pixel = 0;
			++Pixel;

		}
		Row += Pitch;
	}

}

Internal void Win32UpdateWindow(HDC DeviceContext, RECT *WindowRect, int X, int Y, int Width, int Height) {
	int WindowWidth = WindowRect->right - WindowRect->left;
	int WindowHeight = WindowRect->bottom - WindowRect->top;
	StretchDIBits(DeviceContext,
		0, 0, BitmapWidth, BitmapHeight,
		0, 0, WindowWidth, WindowHeight,
		BitmapMemory,
		&BitmapInfo,
		DIB_RGB_COLORS, SRCCOPY);
}

LRESULT CALLBACK Win32MainWindowCallback(HWND Window, UINT Message, WPARAM wParam, LPARAM lParam) {
	LRESULT Result = 0;

	switch (Message) {
	case WM_SIZE:
	{
		RECT ClientRect;
		GetClientRect(Window, &ClientRect);
		int Width = ClientRect.right - ClientRect.left;
		int Height = ClientRect.bottom - ClientRect.top;
		Win32ResizeDIBSection(Width, Height);
		break;
	}
	case WM_DESTROY:
	{
		Running = false;
		break;
	}
	case WM_CLOSE:
	{
		Running = false;
		break;
	}
	case WM_PAINT:
	{
		PAINTSTRUCT Paint;
		HDC DeviceContext = BeginPaint(Window, &Paint);
		int X = Paint.rcPaint.left;
		int Y = Paint.rcPaint.top;
		int Width = Paint.rcPaint.right - Paint.rcPaint.left;
		int Height = Paint.rcPaint.bottom - Paint.rcPaint.top;

		RECT ClientRect;
		GetClientRect(Window, &ClientRect);

		Win32UpdateWindow(DeviceContext, &ClientRect, X, Y, Width, Height);

		EndPaint(Window, &Paint);
		break;
	}
	case WM_ACTIVATEAPP:
	{
		OutputDebugStringA("WM_ACTIVATEAPP\n");
		break;
	}
	default:
	{
		Result = DefWindowProcA(Window, Message, wParam, lParam);
	}
	}
	return Result;
}

int CALLBACK WinMain(HINSTANCE Instance, HINSTANCE PrevInstance, LPSTR CommandLine, int ShowCode) {
	

	WindowClass.style = CS_OWNDC | CS_HREDRAW | CS_VREDRAW;
	WindowClass.lpfnWndProc = Win32MainWindowCallback;
	WindowClass.hInstance = Instance;
	WindowClass.lpszClassName = "HandmadeHeroWindowClass";

	if (RegisterClassA(&WindowClass)) {
		HWND WindowHandle = CreateWindowExA(
			0,
			WindowClass.lpszClassName,
			"Handmade Hero",
			WS_OVERLAPPEDWINDOW | WS_VISIBLE,
			CW_USEDEFAULT, CW_USEDEFAULT,
			CW_USEDEFAULT, CW_USEDEFAULT,
			0, 0, Instance, 0);

		if (WindowHandle) {
			Running = true;
			while (Running) {
				MSG Message;
				BOOL MessageResult = GetMessageA(&Message, 0, 0, 0);
				if (MessageResult > 0) {
					TranslateMessage(&Message);
					DispatchMessageA(&Message);
				}
				else {
					break;
				}
			}
		}
		else {

		}
	}
	else {

	}
	return 0; 
}
When you have "Access Violation Error" then you always run your code under debugger, and when it stops your program because it crashes, examine calls stack and variable values.

In your case errors is this line:
1
int BitmapMemorySize = (BitmapWidth, BitmapHeight) * BytesPerPixel;


Not sure what you think it does, but it only calculates following:
1
int BitmapMemorySize = BitmapHeight * BytesPerPixel;


That's probably not what you want.

There's probably also mistake how you assign values to pixels. Because pixel is typically 4 byte value (RGBA), you need to assign all 4 components at the same time if pointer to pixel is 32-bit integer (which it is in your code). Your code will assign 4 pixels in inner loop. First one will be blue, next 3 will be black.

And there is more - this is wrong:
1
uint32 *Pixel = (uint32 *)&Row;

You are assigning address of pointer to Pixel variable. Instead you simply need to case pointer type, not take address of it.

Similar thing with how you assign address of BitmapMemory pointer to Row pointer. You only need to cast type there, not take address.

Debug, debug and debug more! Use a debugger to step through and examine value of variables.
Thanks a lot. I had been looking at Watch 1 in the debugger forever, I knew it was something I was glazing over again and again. That uint32 *Pixel = (uint32 *)&Row; was just me trying anything I could because I was so frustrated in trying to find the problem.(Worse thing you can do probably)

Edited by OrrsLaw on
Did you fix your BitmapMemorySize calculation?
When it crashes what address does it tries to write? What do you expect is in this address? How do you calculate that? But what is actual value? Can you answer all these questions? Debugging is not just stepping through code and looking at values. It is also process of creating hypothesis how code should behave and verifying why it is or is not happening.

You already are writing all 4 channels.
1
*Pixel = 255;

This code writes all 4 channels. 255 to blue, and 0 to R, G and A.

Edited by Mārtiņš Možeiko on