Simple access violation example.


Hello everyone,

I'm not a complete beginner in computers but I took a long break from them due to life's requirements including children etc...

Anyways, I'm getting into C++ programming and the way I learn is to jump into the deep end and try to stay afloat....

Here's a very simple program and very easy to set up and follow. When I run the program, using Visual Studio 2017 IDE, I get an error as follows...."Exception thrown: read access violation. this was nullptr."

Here is the code which comprises of: Main.cpp file and 2 classes called Game and GameObject with their corresponding cpp and h files.

So.….in main I create a Game object and call the objects runGame() method.
In runGame(), I create a GameObject object, then I invoke Game objects update() method which invokes GameObject objects objUpdate which performs xPos++ and thats where I get the error.

Hope I explained that ok...

Any help would really be appreciated and a big learning curve for me....I've exhausted the google for help but cant find a specific example and solution...

Thank you all!!

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

Main.cpp
--------

#include "Game.h"

Game* game = new Game();

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

	game->runGame();

	delete game;
	game = nullptr;

	return 0;
}

------------------------------------------

Game.cpp
--------

#include "Game.h"

GameObject* player = nullptr;

void Game::runGame()
{
	GameObject* player = new GameObject(5, 5);

	update();
	render();

	delete player;
	player = nullptr;
}

void Game::update()
{
	player->objUpdate();
}

void Game::render()
{
}

------------------------------------------

Game.h
------

#pragma once

#include "GameObject.h"

class Game
{
public:
	void runGame();

	void update();
	void render();
};

------------------------------------------

GameObject.cpp
--------------

#include "GameObject.h"



GameObject::GameObject(int x, int y)
{
	xPos = x;
	yPos = y;
}


GameObject::~GameObject()
{
}

void GameObject::objUpdate()
{
	xPos++;
}

void GameObject::objRender()
{
}
------------------------------------------

GameObject.h
------------

#pragma once

class GameObject
{
public:
	GameObject(int x, int y);
	~GameObject();

	void objUpdate();
	void objRender();

public:
	int xPos;
	int yPos;
};

------------------------------------------
Please do yourself a favor and forget about pointers and also try to avoid global variables.
The game var could be just a local var in main and player could be a member var of Game.
Forget about pointers, in games, lol I don't think so.....And as far as Globals are concerned, i will take that into consideration. This example was part of a bigger project I am writing......But hey, thank you for your humble opinion.....

I just solved the problem too....I am declaring player twice...…

GameObject* player = new player(5, 5);

should be....

player = new GameObject(5, 5)

Forget about pointers, in games, lol I don't think so..


There's nothing special about games that mean they get a free pass. Pointers should only be used when there's no safe, sensible alternative.

In the code above, there is no good use being made of pointers.
The code above is just a simple example to understand pointers further....my bigger project relies more on references....

I am not declaring I know it all, i'm just diving in feet first and learning by my mistakes.....

Your replies are duly noted and very much appreciated.....it's quality advice that I have received here that will further my adventures.....

So thank you once again for your kind words of instruction, I take it all aboard..!!


kind Regards....
Topic archived. No new replies allowed.