创建对对象的引用会导致该引用被破坏。这绝对是奇怪的,因为它似乎是简单的 C++ 功能。另一个奇怪的事情是,问题似乎已经出现而没有更改任何相关代码(我知道)。如果我不得不猜测,我会说这与所涉及的 DirectX 代码有关。
这是代码:
// the header file of the class of the problem object
#pragma once
#include<vector>
#include<Windows.h>
#include<xnamath.h>
#include<string>
using namespace std;
#define float2 XMFLOAT2
#define float4 XMFLOAT4
namespace DXLib
{
struct Sprite
{
char* imagename;
float2 pos;
float rot;
float2 scale;
float4 rgba;
};
struct Rect
{
float2 pos;
float2 size;
float4 color;
};
struct Text
{
std::string string;
float2 pos;
};
class D3DDraw
{
public:
D3DDraw(int maxdepth = 0);
~D3DDraw(void);
void DrawSprite(char* imagename, float2 pos, int depth = 0, float rot = 0.0f, XMFLOAT2 scale = XMFLOAT2(1, 1), float4 rgba = float4(1, 1, 1, 1));
//void DrawRect(float2 pos, float2 size, float4 color);
void DrawDXText(std::string string, float2 pos);
void Clear(void);
public:
std::vector<vector<Sprite>> depths_;
std::vector<Text> texts_;
int maxdepth_; // set on initialize, do not change
float2 spriteoffset_;
};
}
// the header file for the base class for the class in which the problem occurs
#pragma once
#include <windows.h>
#include"D3DCore.h"
#include"D3DRender.h"
#include"D3DDraw.h"
#include"DXInput.h"
#include<cassert>
using namespace DXLib;
const UINT uWindowSizeX = 1000;
const UINT uWindowSizeY = 800;
class Application
{
public:
Application();
~Application();
HRESULT Run();
protected:
virtual void Update(void);
private:
static LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam );
protected:
D3DDraw m_cDraw;
DXInput m_cInput;
private:
HWND m_hWnd;
D3DCore m_cCore;
D3DRender m_cRender;
};
// the class header file
#pragma once
#include"stdafx.h"
#include"VGraphics.h"
#include"EntityPool.h"
#include"VUI.h"
#include"Manager.h"
class VApp : public Application
{
public:
VApp(void);
~VApp(void);
virtual void Update(void) override;
private:
void Log(double dTime); // prints log info to console every nth frame
private:
int m_iTime;
VGraphics m_cGraphics;
EntityPool m_cPool;
VUI m_cVUI;
Manager m_cManager;
};
// the function in which the problem occurs
void VApp::Update(void)
{
double dTime = (GetTickCount() - m_iTime) / 1000.0;
D3DDraw& rcDraw = m_cDraw; // that object is initialized and just fine
// that reference now points to an uninitialized object
m_cVUI.Tick(m_cInput, m_cDraw, m_cPool, m_cManager);
m_cManager.Tick();
m_cGraphics.Draw(m_cVUI.GetView(), m_cPool, m_cDraw);
Log(dTime);
m_iTime = GetTickCount();
Sleep(15);
}