Handmade Hero»Forums»Code
2 posts
Day 004 Write Access Violation
Edited by Nates3 on Reason: more info again
  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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
#include <Windows.h>
#include <stdint.h>

#define  internal static
#define  local_persist static
#define  global_variable 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;
typedef int64_t int64;



//This is a global for now
global_variable bool Running;
global_variable BITMAPINFO BitmapInfo;
global_variable void* BitmapMemory;
global_variable int BitmapWidth;
global_variable int BitmapHeight;





internal void Win32ResizeDIBsection(int Width, int Height)
{
    // Bulletproof this
    //Maybe don't free the memory first, use the old memory of the buffer 

    //free after it is created, free first is that fails however
    
    if(BitmapMemory)
    {   
        VirtualFree(BitmapMemory, 0, MEM_RELEASE);
    }
    BitmapWidth = Width;
    BitmapHeight = Height;

    //Set the bitmapinfo to define the limits of the buffer
    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;
    
    //Allocate ourselves
    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)
    {
        uint8* Pixel = (uint8*)Row;
        for(int x = 0; y < BitmapWidth; ++x)
        {
            /*                0  1   2  3
            Pixel in memory: BB GG  RR xx
            Little Endian Arcitecture
            0x xxGGBBRR
            >  xxRRGGBB They swapped it, B is first, G is second, R is last
            */
            
            *Pixel = 0; //It throws the exception here
            ++Pixel;


            *Pixel = 0;
            ++Pixel;

            
            *Pixel = 255;
            ++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;

    //Stretches or updates the pixels within the rectangle that has just been changed in size
    StretchDIBits(DeviceContext, 
                  /*
                  x, y, Width, Height,
                  x, y, Width, Height,
                  */
                  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;

//  A switch statement that takes in a message and outputs it in the debugger
//  It also takes in a paint message that once called, it draws a rectangle 
    switch(Message)
    {
        case WM_SIZE:
        {
            RECT ClientRect;
            GetClientRect(Window, &ClientRect);
            int Height = ClientRect.bottom - ClientRect.top;
            int Width = ClientRect.right - ClientRect.left;
        

            Win32ResizeDIBsection(Width, Height);
            OutputDebugStringA("WM_SIZE\n");
        } break;

        case WM_DESTROY:
        {
            //Handle this as an error, recreate window
            Running = false;
            OutputDebugStringA("WM_DESTROY\n");
        }break;
        
        case WM_CLOSE:
        {
            //Handle this as a pop up message to the user
            Running = false;
            OutputDebugStringA("WM_CLOSE\n");
        }break;

        case WM_ACTIVATEAPP:
        {
            OutputDebugStringA("WM_ACTIVATEAPP\n");
        }break;

        case WM_PAINT:
        {
            // Contains a pain struct with a RECT function that draws a rectangle
            // We create a handle and set a few variables
            // We draw a rectangle using the variables from rcpaint
            // We go back and fourth between black and white background when the window is updated
            PAINTSTRUCT Paint;
            RECT ClientRect;
            GetClientRect(Window, &ClientRect);
            HDC DeviceContext = BeginPaint(Window, &Paint);
            int x = Paint.rcPaint.left;
            int y = Paint.rcPaint.top;
            int Height = Paint.rcPaint.bottom - Paint.rcPaint.top;
            int Width = Paint.rcPaint.right - Paint.rcPaint.left;
            Win32UpdateWindow(DeviceContext, &ClientRect, x, y, Width, Height);
            EndPaint(Window, &Paint);
            
        }break;

        default:
        {
//            OutputDebugStringA("Default\n");
            Result = DefWindowProc(Window, Message, WParam, LParam);
        }break;
    }

    return (Result);

}





int CALLBACK WinMain(
    HINSTANCE Instance,
    HINSTANCE PrevInstance,
    LPSTR     CommandLine,
    int       ShowCode)
{
    WNDCLASS WindowClass = {};
    
    WindowClass.style = CS_OWNDC;
    WindowClass.lpfnWndProc = Win32MainWindowCallback;
    WindowClass.hInstance = Instance;
//    HICON     hIcon;
    WindowClass.lpszClassName = "HandMadeHeroWindowClass";
    
    if(RegisterClass(&WindowClass))
    {
        //Creates an overlapping window that is visible and has a border
    HWND WindowHandle = 
        CreateWindowExA(
        0,
        WindowClass.lpszClassName,
        "ThisIsWindowYo",
        WS_OVERLAPPEDWINDOW | WS_VISIBLE | WS_BORDER,
        CW_USEDEFAULT,
        CW_USEDEFAULT,
        CW_USEDEFAULT,
        CW_USEDEFAULT,
        0,
        0,      
        Instance,
        0);

        if(WindowHandle)
        {
            Running = true;   
            while(Running)
            {
                //Loops through messages that are past to windows, it breaks when it recives a quit message
                //This allows the window to be displayed for more than one message
                MSG Message;
                BOOL MessageResult = GetMessage(&Message, 0, 0, 0);
                if(MessageResult > 0)
                {
                    TranslateMessage(&Message);
                    DispatchMessage(&Message);
                }
                else
                {
                 break;   
                }
            }
            
        }
        else
        {
            //Logging
        }

    }
    else 
    {
        //Logging
    }
    return(0);



}


I've been at this problem for an hour and a half now; for some reason when debugging, visual studio throws an exception "write access violation" at line 73 and the window is unresponsive :"(

17 posts
Day 004 Write Access Violation
You made a typo on line 64 making it an infinite loop, take a closer look at what you wrote :)
2 posts
Day 004 Write Access Violation
LOL thank you! It literally took me a few seconds to see what went wrong even with your comment, don't code at 3AM!
I first messed with VirtualAlloc for 30 minutes, then i messed with the pointers, then I spent 30 minutes looking at code on google, the last thing I did was mess around in the VS memory tab and I took the start and end of the BitmapMemory and got a size of 4,104,208 when the size was 4,101,120, then I saw your comment. I was on the right path, I almost beat you to it lol.