0

I have a class like this:

class MyClass
{
public:
    MyClass();
    ~MyClass();
    void addPlayer(Player* player);
private:
    Player* p_player;
}

The method addPlayer tries to accomplish the following:

p_player = player;

The idea is to store the information about an object of type Player in order to have access to dynamically check its position. This solution, however, results in "Access violation writing location". Using:

Player* _player = player;

In the function will not crash, but this means I'm not storing the pointer in the class itself to use in other functions of that class.

EDIT: The issue is most likely a problem related with somewhere else in the project. I will redo a part of the project from scratch more carefully. Thank you for the answers.

Grabz
  • 65
  • 7
  • With the code you provide it is not possible to tell what the problem is. I would guess you are passing some un-reserved piece of memory to your variable, but it is just a guess until you show from _where_ do you call the add_player function. – cnluzon Apr 11 '14 at 17:52
  • The variable is `_player` or `p_player`. Do you call the constructor to assign memory for a `player` or not? – Stathis Andronikos Apr 11 '14 at 17:52
  • Okay, I will update the main post momentarily. It is a somewhat complicated engine that I have (it's a school project), so I'll try to explain what it's supposed to do as best as possible. – Grabz Apr 11 '14 at 17:55
  • Compile with all warnings & debug info (e.g. `g++ -Wall -g`). Use a debugger (e.g. `gdb`) and a memory leak detector (e.g. [valgrind](http://valgrind.org/)...) – Basile Starynkevitch Apr 11 '14 at 18:00
  • The variable `player` is being assigned memory using `new`. With the two examples of the `addPlayer` function I wanted to show that the first one, using a class variable doesn't work, but if you create a pointer inside the function instead, you can make it point at `player`. – Grabz Apr 11 '14 at 18:36

2 Answers2

0

What is your Player* raw pointer? Is it an owning pointer? Is it an observing pointer?

If it is an owning pointer, you should not have just a raw pointer inside your class: you'd better use a smart pointer, like unique_ptr (or shared_ptr):

#include <memory> // for unique_ptr

class MyClass {
...
private:
    std::unique_ptr<Player> m_player;
};

You may want to read about The Rule of Three for more information about class copies and proper resource management in C++.

If it is an observing pointer, then raw pointers are fine; but make sure that the observed Player has a lifetime that exceeds that of the MyClass instance that references it.

Community
  • 1
  • 1
Mr.C64
  • 41,637
  • 14
  • 86
  • 162
  • It's an observing pointer. The object Player is added to a vector of objects, so it is not being destroyed at any time, only created. – Grabz Apr 11 '14 at 18:30
0

Sounds like you have:

void MyClass::addPlayer(Player* player)
{
   Player _player = player;
}

Change it to:

void MyClass::addPlayer(Player* player)
{
   p_player = player;
}
R Sahu
  • 204,454
  • 14
  • 159
  • 270