C++ C++;对象引用损坏
创建对对象的引用会导致该引用损坏。这是非常奇怪的,因为它似乎是简单的C++功能。另一件奇怪的事情是,这个问题似乎没有对任何相关代码进行更改(我知道)。如果让我猜的话,我会说这与所涉及的DirectX代码有关 代码如下:C++ C++;对象引用损坏,c++,object,reference,C++,Object,Reference,创建对对象的引用会导致该引用损坏。这是非常奇怪的,因为它似乎是简单的C++功能。另一件奇怪的事情是,这个问题似乎没有对任何相关代码进行更改(我知道)。如果让我猜的话,我会说这与所涉及的DirectX代码有关 代码如下: // the header file of the class of the problem object #pragma once #include<vector> #include<Windows.h> #include<xnamath.h&g
// 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);
}
//问题对象类的头文件
#布拉格语一次
#包括
#包括
#包括
#包括
使用名称空间std;
#定义float2 XMFLOAT2
#定义float4 XMFLOAT4
名称空间DXLib
{
结构精灵
{
字符*图像名称;
2个位置;
漂浮腐烂;
浮动2标度;
4-rgba;
};
结构矩形
{
2个位置;
2号;
4色;
};
结构文本
{
std::字符串;
2个位置;
};
D3DDraw类
{
公众:
D3DDraw(int-maxdepth=0);
~D3DDraw(无效);
void DrawSprite(char*imagename,float2 pos,int depth=0,float rot=0.0f,XMFLOAT2 scale=XMFLOAT2(1,1),float4 rgba=float4(1,1,1));
//void DrawRect(float2位置、float2大小、float4颜色);
void DrawDXText(std::string string,float2 pos);
无效清除(无效);
公众:
向量深度;
向量文本;
int maxdepth;//初始化时设置,不更改
float2 spriteoffset;
};
}
//发生问题的类的基类的头文件
#布拉格语一次
#包括
#包括“D3DCore.h”
#包括“D3DRender.h”
#包括“D3DDraw.h”
#包括“DXInput.h”
#包括
使用名称空间DXLib;
const UINT-uWindowSizeX=1000;
常数uWindowSizeY=800;
班级申请
{
公众:
应用程序();
~Application();
HRESULT Run();
受保护的:
虚拟void更新(void);
私人:
静态LRESULT回调WndProc(HWND HWND、UINT uMsg、WPARAM WPARAM、LPARAM LPARAM);
受保护的:
D3DDraw m_cDraw;
DX输入m_输入;
私人:
HWND m_HWND;
D3DCore m_!cCore;
D3德伦德·穆克伦德;
};
//类头文件
#布拉格语一次
#包括“stdafx.h”
#包括“VGraphics.h”
#包括“EntityPool.h”
#包括“VUI.h”
#包括“Manager.h”
VApp类:公共应用
{
公众:
VApp(无效);
~VApp(无效);
虚拟作废更新(作废)覆盖;
私人:
void Log(double dTime);//每第n帧将日志信息打印到控制台
私人:
国际时间;
VGraphics MU cGraphics;
EntityPool mu cPool;
VUI m_cVUI;
经理m_cManager;
};
//出现问题的函数
void VApp::更新(void)
{
双数据时间=(GetTickCount()-m_iTime)/1000.0;
D3DDraw&rcDraw=m_cDraw;//该对象已初始化,一切正常
//该引用现在指向一个未初始化的对象
m_cVUI.Tick(m_cInput、m_cDraw、m_cPool、m_cManager);
m_cManager.Tick();
m_cGraphics.Draw(m_cVUI.GetView(),m_cPool,m_cDraw);
日志(dTime);
m_iTime=GetTickCount();
睡眠(15);
}
我不知道为什么,但在此处更改访问说明符:
protected:
D3DDraw m_cDraw;
DXInput m_cInput;
公开解决了这个问题,这是一个黑客行为,但我同意:D
无论如何,感谢您尝试帮助好吧,下面是我尝试的SSCCE,但我无法让它失败(可能与通过访问受保护的成员有关):
这段代码毫无意义,可能与问题无关。是的,我知道,还有一点很奇怪,那就是它变成了一个最近的问题,没有对相关代码进行任何更改(我知道)不,我不是说你的问题没有意义,我是说你在这页上写的词没有意义。请制作一个SSCCE,并编辑问题以使其更清晰,然后我们将能够帮助您。一个想法:创建需要引用的变量的堆栈本地副本,并查看是否发生了相同的情况(意味着在堆栈上创建一个D3DDraw&以传递到勾号)。是否有任何原因导致*此指针超出范围?Tick正在尝试存储ref对象的副本吗?这是一个极好的建议silverjam!我试图在调用函数中创建对同一对象的引用,但出现了相同的问题
class Thing
{
public:
int value;
Thing() { value = 42; }
~Thing() { };
};
class Foo
{
public:
Foo() { }
~Foo() { }
public:
void CallMethod()
{
Method();
}
protected:
virtual void Method()
{
}
Thing m_x;
};
class Bar : public Foo
{
public:
Bar() { }
~Bar() { }
virtual void Method() override
{
Thing& x = m_x;
printf("%d\n", x.value);
}
};
int _tmain(int argc, _TCHAR* argv[])
{
Foo f;
Foo& rf = f;
printf("%p\n", &rf);
Bar bar;
Foo& rfoo = bar;
rfoo.CallMethod();
return 0;
}