Access Violation runtime error. Why occurs?
<p>my problem is that when I want to divide the code into smaller functions it throws me a runtime error: access violation.</p>

<p><a href="https://i.stack.imgur.com/qDnIL.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/qDnIL.png" alt="Error dialog image"></a></p>

<p>Since everything is passed by value I have no idea why such an error would occur. There is nothing to do about memory.</p>

<p>General code:</p>

<pre><code>                                // main.cpp
#include "stdafx.h"

#include "SFML/Graphics.hpp"
#include "GameEngine.h"

int main()
{
    sf::RenderWindow* window = new sf::RenderWindow(sf::VideoMode(200, 200), "SFML works!");

    GameEngine game(window);
    game.run();

    delete window;
    return 0;
}

                                // DrawableVertices.h
#pragma once
#include "SFML\Graphics.hpp"
using namespace sf;

class DrawableVertices : public Drawable, public Transformable
{
    VertexArray vertices;
    Texture* pTexture;

public:
    virtual void draw(RenderTarget&amp; target, RenderStates states) const;

    DrawableVertices(VertexArray vertices, Texture* pTexture = nullptr);
    DrawableVertices();

    Texture* getTexture();
    VertexArray* getVertices();
};




                                // DrawableVertices.cpp
#include "stdafx.h"
#include "DrawableVertices.h"

DrawableVertices::DrawableVertices(sf::VertexArray _vertices, sf::Texture* _pTexture)
{
    vertices = _vertices;
    pTexture = pTexture;
}

DrawableVertices::DrawableVertices()
{
    vertices = sf::VertexArray(sf::Points, 1);
    pTexture = nullptr;
}

void DrawableVertices::draw(sf::RenderTarget&amp; target, sf::RenderStates states) const
{
    states.transform *= getTransform();
    states.texture = pTexture;
    target.draw(vertices, states);
}

sf::Texture* DrawableVertices::getTexture() { return pTexture; }
sf::VertexArray* DrawableVertices::getVertices() { return &amp;vertices; }
</code></pre>

<p>Working code (all in one function):</p>

<pre><code>                                //GameEngine.h
#pragma once
#include "DrawableVertices.h"

class GameEngine
{
    RenderWindow* pWindow;

public:
    GameEngine(RenderWindow* window);
    void run();
};

                                //GameEngine.cpp
#include "stdafx.h"
#include "GameEngine.h"

GameEngine::GameEngine(RenderWindow* window)
{
    pWindow = window;
}

void GameEngine::run()
{
    const int X_DIFF = 8;
    const int PLAYER_Y = 24;
    const int PLAYER_X = 48;

    VertexArray playerShape(TrianglesStrip, 4);
    playerShape[0].position = Vector2f(X_DIFF, 0);
    playerShape[0].color = Color(0, 127, 255);

    playerShape[1].position = Vector2f(PLAYER_X - X_DIFF, 0);
    playerShape[1].color = Color(0, 127, 255);

    playerShape[2].position = Vector2f(0, PLAYER_Y);
    playerShape[2].color = Color(0, 0, 255);

    playerShape[3].position = Vector2f(PLAYER_X, PLAYER_Y);
    playerShape[3].color = Color(0, 0, 255);

    DrawableVertices player(playerShape);
    player.setOrigin(PLAYER_X / 2, PLAYER_Y / 2);
    player.setPosition(64, 64);

    while (pWindow-&gt;isOpen())
    {
        Event event;
        while (pWindow-&gt;pollEvent(event))
        {
            if (event.type == Event::Closed)
                pWindow-&gt;close();
        }

        pWindow-&gt;clear();
        pWindow-&gt;draw(player);
        pWindow-&gt;display();
    }

}
</code></pre>

<p>Not working code, throwing access violation while drawing player
[ pWindow->draw(player) line ]</p>

<pre><code>                                //GameEngine.h
#pragma once
#include "DrawableVertices.h"

class GameEngine
{
    RenderWindow* pWindow;
    DrawableVertices player;

    void createPlayer();

public:
    GameEngine(RenderWindow* window);
    void run();
};

                                // GameEngine.cpp
#include "stdafx.h"
#include "GameEngine.h"


GameEngine::GameEngine(RenderWindow* window)
{
    pWindow = window;
    createPlayer();
}

void GameEngine::createPlayer()
{
    const int X_DIFF = 8;
    const int PLAYER_Y = 24;
    const int PLAYER_X = 48;

    VertexArray playerShape(TrianglesStrip, 4);
    playerShape[0].position = Vector2f(X_DIFF, 0);
    playerShape[0].color = Color(0, 127, 255);

    playerShape[1].position = Vector2f(PLAYER_X - X_DIFF, 0);
    playerShape[1].color = Color(0, 127, 255);

    playerShape[2].position = Vector2f(0, PLAYER_Y);
    playerShape[2].color = Color(0, 0, 255);

    playerShape[3].position = Vector2f(PLAYER_X, PLAYER_Y);
    playerShape[3].color = Color(0, 0, 255);

    player = DrawableVertices(playerShape);
    player.setOrigin(PLAYER_X / 2, PLAYER_Y / 2);
    player.setPosition(64, 64);
}


void GameEngine::run()
{

    while (pWindow-&gt;isOpen())
    {
        Event event;
        while (pWindow-&gt;pollEvent(event))
        {
            if (event.type == Event::Closed)
                pWindow-&gt;close();
        }

        pWindow-&gt;clear();
        pWindow-&gt;draw(player);
        pWindow-&gt;display();
    }

}
</code></pre>

<p>What's worse when I display all player's information like position etc. inside run() method it shows the proper result. It means that player is initialised properly and it exists like it is supposed to.</p>

<p>Sorry for a lot of code, but wanted to show everything that could be useful. Obviously my whole code is bigger, I just put the part making problems into a separate project.</p>

<p>// Edit</p>

<p>Yeah, debugger says that pTexture member of player has a bad pointer.</p>

<p><a href="https://i.stack.imgur.com/yojz2.png" rel="nofollow noreferrer">image from debugging working and not working code</a></p>

<p>I suppose that overloading = operator might fix the problem. However, I want to understand how it works that this code works without any problems:</p>

<pre><code>void GameEngine::run()
{
    DrawableVertices player = DrawableVertices(playerShape);
    // ...
    pWindow-&gt;draw(player);
}
</code></pre>

<p>And packing it into a fuction doesn't:</p>

<pre><code>void GameEngine::createPlayer()
{
     // ...
     DrawableVertices player = DrawableVertices(playerShape);
}

void GameEngine::run()
{
    createPlayer();
    // ...
    pWindow-&gt;draw(player);
}
</code></pre>