抛出异常:读取访问冲突。 this-> m_pRenderTarget是nullptr。 C ++

时间:2016-04-30 17:58:22

标签: c++

我不确定错误在哪里。我在youtube上观看视频系列节目,学习如何编写游戏引擎,我承认我不是最好的编码器(我实际上是将这个项目作为一种学习体验开始),这可能就是为什么我可以&# 39;找不到这个错误,但是我已经至少重复了10次图形部分仍然没有找到问题的运气。有人可以帮助找到这个并向我解释这种情况发生的原因和原因吗?

graphics.h
#ifndef _Graphics_H
#define _Graphics_H
//C run time header files 

//ADDITIONAL INCLUDES 
#include "System.h"

//#ifndef _Logger_H
//  #include "Logger.h"
//#endif

#ifndef _2DUTILL_H
    #include "d2dutill.h"
#endif

//forward declrations 
class Window;


//struct 
struct GraphicsData :public SystemData
{
public: 
    GraphicsData(Window* wnd = nullptr);
    Window* pWnd;
};
class Graphics : public System
{
    friend class Engine;
public:
    HRESULT OnResize(UINT width, UINT height);
    ID2D1HwndRenderTarget* GetRenderTarget()            { return m_pRenderTarget; }
    IWICImagingFactory* GetImageFactory()               { return m_pImageFactory; }
    ID2D1SolidColorBrush* GetColorBrush()               { return m_pColorBrush; }
    ID2D1Factory* GetD2DFactory()                       { return m_pD2DFactory; }

protected:
    Graphics(const GraphicsData& data);
    virtual ~Graphics();
    Graphics(const Graphics& other);
    Graphics& operator = (const Graphics& tref);
    bool initialize();
    bool ShutDown();
private: 
//  void* operator new(size_t size);
//  void operator delete(void* pdelete);

    HRESULT CreateDeviceIndependentResorces();
    HRESULT CreateDeviceDependentResorces();

    void BeginDraw();
    HRESULT EndDraw();
    void DiscardDeviceResorces();
    //members
    ID2D1HwndRenderTarget* m_pRenderTarget;
    IWICImagingFactory* m_pImageFactory;
    ID2D1SolidColorBrush* m_pColorBrush;
    ID2D1Factory* m_pD2DFactory;

    Window* m_pWindow;
};
#endif 

由于我喜欢做测试的方式,有些事情还没有实现。

Graphics.cpp

include "Graphics.h"
#include "Window.h"
#ifndef _DeleteMacros_H
    #include "DeleteMacros.h"
#endif

GraphicsData::GraphicsData(Window* wnd)
    :
    SystemData(SystemType::Sys_Graphics),
    pWnd(wnd)
    {}



Graphics::Graphics(const GraphicsData& data)
    :
    System(data),
    m_pRenderTarget(nullptr),
    m_pD2DFactory(nullptr),
    m_pColorBrush(nullptr),
    m_pImageFactory(nullptr),
    m_pWindow(data.pWnd)
{}

//void Graphics::operator new(size_T size)
//{
//  return MEMORYMANAGER->alloc(size);
//}
//void Graphics::operator delete(void* pDelete)
//{
//  MEMORYMANAGER->free(pDelete);
//}



Graphics::~Graphics()
{}

HRESULT Graphics::OnResize(UINT width, UINT height)
{
    //this method can fail and is safe to ignore the warning 
    //error will be returned until the end draw method is called again 

    if (m_pRenderTarget)
    return m_pRenderTarget->Resize(D2D1::SizeU(width, height));
    else return S_FALSE;
}

bool Graphics::initialize()
{
    System::Initialize();
    HRESULT hr = CreateDeviceIndependentResorces();
    if (FAILED(hr))
        return false;
    hr = CreateDeviceDependentResorces();
    if (FAILED(hr))
        return false;

//  Logger::log(_T("initializing Graphics Complete"),LOGTYPE_INFO, false);

    return true;
}

bool Graphics::ShutDown()
{
    System::ShutDown();

    SafeRelease(m_pD2DFactory);
    SafeRelease(m_pColorBrush);
    SafeRelease(m_pImageFactory);
    SafeRelease(m_pRenderTarget);
    return true;

}

HRESULT Graphics::CreateDeviceIndependentResorces()
{
    HRESULT hr = S_OK;
// create Direct2D factory
    hr = D2D1CreateFactory(D2D1_FACTORY_TYPE_SINGLE_THREADED, &m_pD2DFactory);
    hr = CoCreateInstance(CLSID_WICImagingFactory, NULL, CLSCTX_INPROC_SERVER, IID_IWICImagingFactory, (LPVOID*)&m_pImageFactory);

    return hr;
}

HRESULT Graphics::CreateDeviceDependentResorces()
{
    HRESULT hr = S_OK;
    if (m_pRenderTarget==nullptr)
    {
        //get window handel
        HWND hWnd = m_pWindow->GetWindowHandle();

        RECT rc;
        GetClientRect(hWnd, &rc);
        D2D1_SIZE_U size = D2D1::SizeU(rc.right - rc.left, rc.bottom - rc.top);

        // create direct2d render target 
        hr = m_pD2DFactory->CreateHwndRenderTarget(D2D1::RenderTargetProperties(), D2D1::HwndRenderTargetProperties(hWnd, size), &m_pRenderTarget);
        if (FAILED(hr))
            return hr;

        //create a brush
        hr = m_pRenderTarget->CreateSolidColorBrush((D2D1::ColorF) D2D1::ColorF::Black, &m_pColorBrush);
        if (FAILED(hr))
            return hr;
    }
    return hr;
}


void Graphics::DiscardDeviceResorces()
{
    SafeRelease(m_pRenderTarget);
}

void Graphics::BeginDraw()
{

        m_pRenderTarget->BeginDraw();
        m_pRenderTarget->Clear(D2D1::ColorF(D2D1::ColorF::White));
}

HRESULT Graphics::EndDraw()
{
    return m_pRenderTarget->EndDraw();

}

m_pRenderTarget->BeginDraw();

引发异常

1 个答案:

答案 0 :(得分:0)

1)更改构造函数,以便实际将成员变量设置为指向某些内容(例如,活动对象)

2)将您的成员指针包裹到智能指针(例如std::unique_ptr<>