TerenceChill
TerenceChill

Reputation: 139

Displaying text with DirectX 9 crashes after several calls

I want to display text with DirectX 9 but after thousands of calls the application crashes. In the future the text should be changeable, but in the example below, the text is everytime the same. The stack trace gives not much information, the crash occurs in some other part of the program due an access violation.

The header:

(...)

class QD3DWidget;

class D3DText : public D3DPrimitive
{

  public:
    D3DText(IDirect3DDevice9 * device);
    virtual ~D3DText();

    virtual bool CreateBuffers();
    virtual void ReleaseBuffers();
    virtual void Draw();

  protected:
    IDirect3DDevice9 * Device();

  private:

    struct TextData {
        Vector2 pos;
        std::string text;
    };

    QD3DWidget * m_pQD3DWidget;
    IDirect3DDevice9 * m_pD3DDevice;
    ID3DXFont *font;

};
#endif

And the implementation:

(...)

D3DText::D3DText(IDirect3DDevice9 * device)
    : D3DPrimitive(device),
    m_pQD3DWidget(0),
    m_pD3DDevice(device),
    font(0)
{
}

D3DText::~D3DText()
{
    ReleaseBuffers();
}

void D3DText::Draw()
{
    if (font)
    {
        RECT fRectangle;
        SetRect(&fRectangle, 0, 0, 1000, 440);
        std::string message = "This is some generic message to\ndisplay on the screen";
        font->DrawTextA(NULL, message.c_str(), -1, &fRectangle, DT_LEFT, D3DCOLOR_XRGB(0, 0, 0));
    }
}

void D3DText::ReleaseBuffers()
{
    if (font) { 
        font->Release(); font = 0; 
    }
}

bool D3DText::CreateBuffers()
{
    font = 0;
    HRESULT hr = D3DXCreateFont(m_pD3DDevice, 22, 0, FW_NORMAL, 1, false, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS,
        ANTIALIASED_QUALITY, FF_DONTCARE, TEXT("Arial"), &font);
    if (!SUCCEEDED(hr))
        return false;
    
    return true;
}

IDirect3DDevice9 * D3DText::Device()
{
    return m_pD3DDevice;
}

Upvotes: 0

Views: 63

Answers (0)

Related Questions