每当我运行程序时,我都会在两条单独的代码行上引发异常,如果我注释掉其中的一行,则问题会跳到另一行,但都引用同一个变量。
GameScreenManager.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);
”引发未处理的异常:读取访问冲突。 此是nullptr。发生了
答案 0 :(得分:0)
显然,当您调用Render()或update()时,mCurrentScreen字段为NULL。通过检查mCurrentScreen的NULL来保护调用:
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。
答案 1 :(得分:0)
碰巧的是,我实际上错过了设置mCurrentScreen的代码行,并且switch语句未正确传递,因此未触发。另外,在我的任何cpp文件中均未正确设置mRenderer变量,因此无论何时尝试使用它,都会返回“ Invalid Renderer”。以下是有效的代码,感谢大家为我提供了正确的指导;
#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;
}
}