【发布时间】:2016-05-11 01:04:35
【问题描述】:
我的问题是,当我想将代码分成更小的函数时,它会引发运行时错误:访问冲突。
由于一切都是按值传递的,我不知道为什么会发生这样的错误。与记忆无关。
通用代码:
// 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) line ]
//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();
}
}
更糟糕的是,当我在 run() 方法中显示所有玩家的信息(如位置等)时,它会显示正确的结果。这意味着播放器已正确初始化,并且按照应有的方式存在。
抱歉有很多代码,但想展示所有可能有用的东西。显然我的整个代码比较大,我只是把产生问题的部分放到一个单独的项目中。
// 编辑
是的,调试器说播放器的 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);
}
【问题讨论】:
-
单步调试代码时调试器会告诉你什么?
-
调试器告诉你什么是什么意思?调试器输出准确地说明了屏幕截图所说的内容:“AccessViolationBug.exe 中的 0x0FBA8BC6 (sfml-graphics-d-2.dll) 引发的异常:0xC0000005:访问冲突读取位置 0xCCCCCCE4。”转到“Locals”,播放器看起来有效。
-
(抱歉,我错过了这张图片,因为你的 alt 文本做得很差,几乎看不到它。)在 AV 发生之前单步执行代码,并检查那里的变量。有些事情不正确,或者 AV 不会发生。我怀疑错误是否在 sfml 中,这意味着您的代码中的某些内容导致了在调用 DLL 之前没有被注意到的问题。堆栈损坏、无效指针、内存覆盖或类似情况很可能是原因。你比我们更容易找到它,因为你拥有所有的碎片。
-
该图像没有在调试器中逐步执行代码。这是在 AV 引起 之后发生的异常对话框。您需要使用调试器在 AV 之前单步执行代码以查看导致它的原因。