每当我运行程序时,我都会在两行单独的代码上抛出异常,如果我将其中一行注释出来,问题会跳转到另一行,但两者都引用相同的变量。
游戏屏幕管理器.cpp:
#pragma once
#include <SDL.h>
#include "Commons.h"
#include "GameScreenManager.h"
#include "GameScreenLevel1.h"
#include "GameScreen.h"
GameScreenManager::GameScreenManager(SDL_Renderer* renderer, SCREENS startScreen)
{
SDL_Renderer* mRenderer;
mRenderer = NULL;
ChangeScreen(startScreen);
}
GameScreenManager::~GameScreenManager()
{
mRenderer = NULL;
delete mCurrentScreen;
mCurrentScreen = NULL;
}
void GameScreenManager::Render()
{
mCurrentScreen->Render();
}
void GameScreenManager::update(float deltaTime, SDL_Event e)
{
mCurrentScreen->Update(deltaTime, e);
}
void GameScreenManager::ChangeScreen(SCREENS newScreen)
{
//Clear up the old screen
if (mCurrentScreen != NULL)
{
delete mCurrentScreen;
}
GameScreenLevel1* tempScreen;
switch (newScreen)
{
case SCREEN_INTRO:
break;
case SCREEN_MENU:
break;
case SCREEN_LEVEL1:
tempScreen = new GameScreenLevel1(mRenderer);
mCurrentScreen = (GameScreen*)tempScreen;
tempScreen = NULL;
break;
case SCREEN_LEVEL2:
break;
case SCREEN_GAMEOVER:
break;
case SCREEN_HIGHSCORES:
break;
default:
break;
}
}
GameScreenManager.h:
#pragma once
#ifndef _GAMESCREENMANAGER_H
#define _GAMESCREENMANAGER_H
#include <SDL.h>
#include "Commons.h"
class GameScreen;
class GameScreenManager
{
private:
SDL_Renderer* mRenderer;
GameScreen* mCurrentScreen;
public:
GameScreenManager(SDL_Renderer* renderer, SCREENS startScreen);
~GameScreenManager();
void Render();
void update(float deltaTime, SDL_Event e);
void ChangeScreen(SCREENS newScreen);
};
#endif //_GAMESCREENMANAGER_H
目前问题出现在第 24 行;
mCurrentScreen->Render();
但是,如果我注释掉该行,则它出现在第 29 行:
mCurrentScreen->Update(deltaTime, e);
"引发未处理的异常:读取访问冲突。这是无效的。发生">
显然,当你调用 Render(( 或 update(( 时,mCurrentScreen 字段是空的。通过检查 mCurrentScreen 的空来保护呼叫:
void GameScreenManager::Render()
{
if (mCurrentScreen != NULL)
mCurrentScreen->Render();
}
void GameScreenManager::update(float deltaTime, SDL_Event e)
{
if (mCurrentScreen != NULL)
mCurrentScreen->Update(deltaTime, e);
}
或者确保在调用 Render(( 或 Update(( 之前设置了 mCurrentScreen。
巧的是,我实际上错过了设置mCurrentScreen的一行代码,并且switch语句没有正确传递,因此它没有触发。此外,mRenderer 变量在我的任何 cpp 文件中都没有正确设置,因此每当我尝试使用它时都会返回"无效渲染器"。以下是工作代码,感谢大家的帮助,它引导我朝着正确的方向前进;
#pragma once
#include <SDL.h>
#include "Commons.h"
#include "GameScreenManager.h"
#include "GameScreenLevel1.h"
#include "GameScreen.h"
GameScreenManager::GameScreenManager(SDL_Renderer* renderer, SCREENS startScreen)
{
mRenderer = renderer;
GameScreen* mCurrentScreen = NULL;
ChangeScreen(startScreen);
}
GameScreenManager::~GameScreenManager()
{
mRenderer = NULL;
delete mCurrentScreen;
mCurrentScreen = NULL;
}
void GameScreenManager::Render()
{
mCurrentScreen->Render();
}
void GameScreenManager::Update(float deltaTime, SDL_Event e)
{
mCurrentScreen->Update(deltaTime, e);
}
void GameScreenManager::ChangeScreen(SCREENS newScreen)
{
//Clear up the old screen
if (mCurrentScreen != NULL)
{
delete mCurrentScreen;
}
GameScreenLevel1* tempScreen;
switch (newScreen)
{
case SCREEN_INTRO:
break;
case SCREEN_MENU:
break;
case SCREEN_LEVEL1:
tempScreen = new GameScreenLevel1(mRenderer);
mCurrentScreen = (GameScreen*)tempScreen;
tempScreen = NULL;
break;
case SCREEN_LEVEL2:
break;
case SCREEN_GAMEOVER:
break;
case SCREEN_HIGHSCORES:
break;
default:
break;
}
}