2013-02-22 193 views
-5

創建對對象的引用會導致引用被損壞。這是非常奇怪的,因爲它似乎是簡單的C++功能。另一個奇怪的問題是,這個問題似乎沒有改變任何相關的代碼(我知道)。如果我不得不猜測,我會說這與涉及的DirectX代碼有關。C++對象引用損壞

下面是代碼:

// 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); 
} 
+5

這個代碼是沒有意義的,可能」不是個與這個問題有關。做一個[簡短,自包含,可編譯的例子](http://sscce.org/) – 2013-02-22 21:24:47

+0

是的,我知道,還有什麼奇怪的是,它成爲一個最近的問題,沒有任何相關代碼的變化(即我'm知道) – TheResolute 2013-02-22 21:28:01

+1

不,我不是說你的_problem_沒有任何意義,我的意思是你放在這個頁面上的單詞是沒有意義的。請創建一個SSCCE,並將問題編輯得更清楚,然後我們將能夠爲您提供幫助。 – 2013-02-22 21:30:06

回答

0

我不知道爲什麼,但在這裏改變了訪問說明符:

protected: 
    D3DDraw m_cDraw; 
    DXInput m_cInput; 

public解決了這個問題,這是一個黑客,但我沒事與那:D

無論如何試圖幫助

0

好了,這是我在SSCCE嘗試,但我不能讓它失敗(可能有事情做與訪問受保護的成員通過pointer to the wrong kind of object):

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; 
}