Home > Enterprise >  Exception thrown: write access violation. this->**m_buffer** was 0x111011101110112
Exception thrown: write access violation. this->**m_buffer** was 0x111011101110112

Time:03-06

Im following this (https://www.udemy.com/course/free-learn-c-tutorial-beginners/learn/lecture/1838486#announcements ) tutorial and im stuck. It seem that windows throws that exception and it cant be handled. I think I try write something to some wrong place but cant see it.

Heres the line that causes the problem.

m_buffer[(y * SCREEN_WIDTH) x] = color;

And here is everything else

//====================================main.cpp=======================================

#include<iostream>

#include "Screen.h"

#include<SDL.h>

using namespace std;


int main(int argc, char* argv[]) {

    Screen screen;

    if (!screen.init()) {
        cout << "Error initialising SDL." << endl;
    }
    
    
    
    
    while (true) {
        // update particles
        // draw particles
    
        
        for (int y = 0; y < Screen::SCREEN_HEIGHT; y  ) {
            for (int x = 0; x < Screen::SCREEN_WIDTH; x   ){
                screen.setPixel(x, y, 128, 0, 255);
            }
        }
        
    
        //Draw the screen
        screen.update();

        // see for messages/events
        if (!screen.processEvents()) {
            break;
        }
    }

    
    screen.close();

    return 0;
}

//==========================================================================================
screen.h

#ifndef SCREEN_H_
#define SCREEN_H_


#include<SDL.h>
#include<iostream>

class Screen
{
public:
    const static int SCREEN_WIDTH = 800;
    const static int SCREEN_HEIGHT = 600;

private:
    SDL_Window* m_window;
    SDL_Renderer* m_renderer;
    SDL_Texture* m_texture;
    Uint32* m_buffer;

public:
    Screen();
    bool init();
    void update();
    void setPixel(int x, int y, Uint8 red, Uint8 green, Uint8 blue);
    bool processEvents();
    void close();


};

#endif // !SCREEN_H_

//=======================screen.cpp======================================================

#include "Screen.h"



Screen::Screen() : m_window(NULL), m_renderer(NULL), m_texture(NULL), m_buffer(NULL)
{

}



bool Screen::init() {

    if (SDL_Init(SDL_INIT_VIDEO) < 0) {
        return false;
    }

    m_window = SDL_CreateWindow("Particle Fire Explosion",
        SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);    //-----------------SDL_WINDOW_HIDDEN   SDL_WINDOW_SHOWN

    if (m_window == 0) {
        SDL_Quit();
        return false;
    }
    m_renderer = SDL_CreateRenderer(m_window, -1, SDL_RENDERER_PRESENTVSYNC);
    m_texture = SDL_CreateTexture(m_renderer,
        SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_STATIC, SCREEN_WIDTH, SCREEN_HEIGHT);

    if (m_renderer == NULL) {

        SDL_DestroyWindow(m_window);
        SDL_Quit();
        return false;
    }

    if (m_texture == NULL) {

        SDL_DestroyWindow(m_window);
        SDL_DestroyRenderer(m_renderer);
        SDL_Quit();
        return false;
    }
    
    Uint32* buffer = new Uint32[SCREEN_WIDTH * SCREEN_HEIGHT];
        
    memset(buffer, 0xFF, SCREEN_WIDTH * SCREEN_HEIGHT * sizeof(Uint32));     
    
    return true;
}


bool Screen::processEvents() {
    SDL_Event event;
    while (SDL_PollEvent(&event)) {
        if (event.type == SDL_QUIT)
        {
            return false;
        }
    }
    return true;
}

void Screen::setPixel(int x, int y, Uint8 red, Uint8 green, Uint8 blue) {
    
    Uint32 color = 0;

    color  = red;
    color <<= 8;
    color  = green;
    color <<= 8;
    color  = blue;
    color <<= 8;
    color  = 0xFF;
    

    m_buffer[(y * SCREEN_WIDTH)   x] = color;
}


void Screen::update() {
    SDL_UpdateTexture(m_texture, NULL, m_buffer, SCREEN_WIDTH * sizeof(Uint32));
    SDL_RenderClear(m_renderer);
    SDL_RenderCopy(m_renderer, m_texture, NULL, NULL);
    SDL_RenderPresent(m_renderer);
}


void Screen::close() {
    delete[] m_buffer;
    SDL_DestroyRenderer(m_renderer);
    SDL_DestroyTexture(m_texture);
    SDL_DestroyWindow(m_window);
    SDL_Quit();
}

//==========================================================================================

Maybe someone can see what is the broblem?

CodePudding user response:

this line is wrong

 Uint32* buffer = new Uint32[SCREEN_WIDTH * SCREEN_HEIGHT];

you mean

m_buffer = new Uint32[SCREEN_WIDTH * SCREEN_HEIGHT];

CodePudding user response:

Your Screen::init() method never sets m_buffer, so when setPixel() tries to dereference it, it is dereferencing a NULL pointer, which invokes undefined behavior.

  •  Tags:  
  • c
  • Related