I am working on a small game in SFML, I created a game class that will store various classes; for example, the player class.
I initialized the class on the heap with "new" keyword, but after hours of trying to figure out why my player class was going null after a certain scope. I decided not to initialize the game on the heap and after I did that, everything was fine.
Can someone please explain why this was happening. By the way, I am not creating the game on the heap anymore, I am showing the code I previously had when I was getting the null reference.
int main()
{
    sf::RenderWindow renderWindow(sf::VideoMode(640, 480), "Dungeon Run");
    //Asset test;
    //test.CreateAsset("GameAssets/Player.png");
    sf::Event event;
    renderWindow.setKeyRepeatEnabled(true);
    Game* game = new Game(renderWindow);
// Player initialized here.
    game->Initialize(); 
    while (renderWindow.isOpen()) 
    {
// Game->Player goes null in here.
        while (renderWindow.pollEvent(event))
        {
            if (event.type == sf::Event::EventType::Closed)
            {
                renderWindow.close();
            }
            //set window to random color to check if working
            if (event.type == sf::Event::EventType::KeyPressed) {
                if (sf::Keyboard::isKeyPressed(sf::Keyboard::A)) {
                }
                else if (sf::Keyboard::isKeyPressed(sf::Keyboard::W)) {
                }
                else if (sf::Keyboard::isKeyPressed(sf::Keyboard::S)) {
                }
                else if (sf::Keyboard::isKeyPressed(sf::Keyboard::D)) {
                }
                else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Escape)) {
                    renderWindow.close();
                }
            }
            else if (event.type == sf::Event::EventType::MouseButtonPressed) {
                if (sf::Mouse::isButtonPressed(sf::Mouse::Left)) {
                }
                else if (sf::Mouse::isButtonPressed(sf::Mouse::Right)) {
                }
            }
            renderWindow.clear();
            game->Draw(); // Here player was null when I tried to draw the sprite for the player.
            renderWindow.display();
        }
    }
delete game;
game = nullptr;
}
Player class:
class Player
{
public:
    Player();
    Player(std::string name) 
        : name(name) 
    {
    }
    std::string name = "";
    double health = 100;
    double stamina = 100;
    int level = 1;
    Asset sprite;
    void attack();
    ~Player();
};
Asset class:
Asset::Asset() {
}
void Asset::CreateAsset(std::string path) {
    if (!texture.loadFromFile(path)) {
        std::cout << "Failed to load texture from file: " << path << std::endl;
    }
    sprite.setTexture(texture);
}
sf::Sprite& Asset::GetSprite() {
    return sprite;
}
Asset::~Asset() {
}
Game.h and Game.cpp:
class Game {
private:
    Player player;
    sf::RenderWindow* window;
public:
    Game(sf::RenderWindow& window);
    void Initialize();
    void Upadte();
    void Draw();
    ~Game();
};
Game::Game(sf::RenderWindow& window) {
    this->window = &window;
    player = Player("Player");
}
void Game::Initialize() {
    player.sprite.CreateAsset("GameAssets/player.png");
}
void Game::Upadte() {
}
void Game::Draw() {
    window->draw(player.sprite.GetSprite()); // Drawing his sprite
}
Game::~Game() {
}
 
     
    