访问冲突运行时错误。为什么会发生?

Access Violation runtime error. Why occurs?

我的问题是,当我想将代码分成更小的函数时,它抛出一个 运行时间错误:访问冲突。

既然一切都是按值传递的,我不知道为什么会出现这样的错误。跟内存没关系。

通用代码:

                                // 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& 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& 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 &vertices; }

工作代码(全部在一个函数中):

                                //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->isOpen())
    {
        Event event;
        while (pWindow->pollEvent(event))
        {
            if (event.type == Event::Closed)
                pWindow->close();
        }

        pWindow->clear();
        pWindow->draw(player);
        pWindow->display();
    }

}

代码无效,绘制播放器时抛出访问冲突 [ pWindow->draw(player) 线]

                                //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->isOpen())
    {
        Event event;
        while (pWindow->pollEvent(event))
        {
            if (event.type == Event::Closed)
                pWindow->close();
        }

        pWindow->clear();
        pWindow->draw(player);
        pWindow->display();
    }

}

更糟糕的是,当我在 运行() 方法中显示所有玩家的位置等信息时,它显示了正确的结果。这意味着播放器已正确初始化并且它应该存在。

抱歉代码太多,但想展示所有可能有用的东西。显然我的整个代码比较大,我只是把出问题的部分放在一个单独的项目中。

// 编辑

是的,调试器说播放器的 pTexture 成员有一个错误的指针。

image from debugging working and not working code

我想重载 = 运算符可能会解决问题。但是,我想了解这段代码是如何工作的,没有任何问题:

void GameEngine::run()
{
    DrawableVertices player = DrawableVertices(playerShape);
    // ...
    pWindow->draw(player);
}

并且将其打包到函数中不会:

void GameEngine::createPlayer()
{
     // ...
     DrawableVertices player = DrawableVertices(playerShape);
}

void GameEngine::run()
{
    createPlayer();
    // ...
    pWindow->draw(player);
}

错误(如果我没记错的话)很简单

在这个构造函数中

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

你自己复制pTexture;所以pTexture开始未定义并保持未定义。

我想你的意图是

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

p.s.: 抱歉我的英语不好