JensB
JensB

Reputation: 939

Why is the debugger throwing "read access violation. this was nullptr" exception?

First time posting here, so sorry if this is a beginner's problem. I am trying to create a Snake class for a simple game I'm making but the debugger keeps throwing weird exceptions.

Every time I debug the program, it takes me into a 'vector' file, points me to a certain line and says: "read access violation.'this' was nullptr". When I run it without debugging it just terminates with the message: "exited with code -1073741819.".

I've been trying to solve this for hours but still have no idea what the problem is. Is there something wrong with my code, or does the problem not lie there? Thanks in advance!

In Snake.h:

#include <vector>

class Snake{
private:
    std::vector<sf::RectangleShape> body;
    int speed;
public:
    Snake();

    void draw_body(sf::RenderWindow& windowRef);
    void move(sf::RenderWindow& windowRef);

    void set_speed(int s);

};

In Snake.cpp: (I haven't finished Snake::move() yet)

#include "Snake.h"
#include <SFML\Graphics.hpp>

Snake::Snake() : speed{ 10 }, body{ sf::RectangleShape { sf::Vector2f(50.f,50.f) } }{
    body.at(0).setPosition(50.f, 50.f);
}
void Snake::draw_body(sf::RenderWindow& windowRef) {
    for (sf::RectangleShape rect : body) {
        windowRef.draw(rect);
    }
}
void Snake::set_speed(int s) {
    speed = s;
}
void Snake::move(sf::RenderWindow& windowRef) {
    ;
}

In main.cpp:

#include <SFML\Graphics.hpp>
#include <iostream>

#include "Snake.h"
class Snake;

int main() {
    sf::RenderWindow window(sf::VideoMode(800, 800), "MY window", sf::Style::Close | sf::Style::Titlebar);
    Snake* snake{};

    while (window.isOpen()) 
    {
        sf::Event event;

        while (window.pollEvent(event))
        {   
        }




        window.clear(sf::Color::White);

        snake->draw_body(window);

        window.display();




    }
    return 0;
}

Upvotes: 3

Views: 1172

Answers (2)

Alex
Alex

Reputation: 1924

This line:

Snake* snake{};

Does not initialize an instance of the Snake class, but a pointer to an instance. Pointers are integers at the assembly level and are initialized to 0 by default, the same numeric value as NULL in C and nullptr in modern C++ (these are all identical at the assembly level).

To fix this, you either need to allocate an instance of Snake on the heap with new and delete it when you're done using it or allocate the instance on the stack by removing the pointer.

Upvotes: 1

Christophe
Christophe

Reputation: 73376

This is because your code has undefined behavior (UB). It can work by coincidence, it can fail without any symptom, or the weirdest things can happen.

The debugger takes in general extra care, adding a lot of controls. And it fortunately spotted the use of a null pointer. In fact, this line does not what you think it does:

Snake* snake{};

No Snake is created. snake is a pointer to Snake. So you've just initialized a pointer. What do you think its value is?

THe best option here is to get rid of the pointer:

Snake snake{};   // no *,  so now you have a real snake object ready to use

...
snake.draw_body(window);   // no ->  since it's no longer a pointer

Upvotes: 4

Related Questions