2

我明白了

Debug assertion failed.
p!=0

它指向:

    _NoAddRefReleaseOnCComPtr<T>* operator->() const throw()
    {
        ATLASSERT(p!=NULL);
        return (_NoAddRefReleaseOnCComPtr<T>*)p;
    }

在“atlcomcli.h”中

据我了解,这意味着我忘记在某处初始化指针,但它们似乎都已初始化。

当我使用普通指针而不是“CComPtr”时,它会在 D3DFont.cpp 中的“D3DFont::Draw”中的“font->DrawTextA”处引发“访问冲突读取位置”

//D3DFont.h:
#include <D3DX10.h>
#include <atlbase.h>
#include <string>

class D3DFont
{
public:
    D3DFont(void);
    ~D3DFont(void);

    bool Create(ID3D10Device *device, std::string name, int width,
        int height, int weight, int mipLevels, bool italic, BYTE charset,
        BYTE quality, BYTE pitchAndFamily);
    void Draw(LPD3DX10SPRITE sprite, std::string text, int charCount,
        LPRECT rect, UINT format, D3DXCOLOR color);

private:
    CComPtr<ID3DX10Font> font;
};

//D3DFont.cpp:
#include "D3DFont.h"

D3DFont::D3DFont(void){} 
D3DFont::~D3DFont(void){}

bool D3DFont::Create( ID3D10Device *device, std::string name,
    int width, int height, int weight, int mipLevels, bool italic,
    BYTE charset, BYTE quality, BYTE pitchAndFamily )
{
    D3DX10_FONT_DESC fd;
    ZeroMemory(&fd, sizeof(D3DX10_FONT_DESC));

    fd.Height = height;
    fd.Width = width;
    fd.Weight = weight;
    fd.MipLevels = mipLevels;
    fd.Italic = italic;
    fd.CharSet = charset;
    fd.Quality = quality;
    fd.PitchAndFamily = pitchAndFamily;

    strcpy_s(fd.FaceName, name.c_str());

    // INITIALIZING FONT HERE
    D3DX10CreateFontIndirect(device, &fd, &font);

    return true;
}

void D3DFont::Draw( LPD3DX10SPRITE sprite, std::string text,
    int charCount, LPRECT rect, UINT format, D3DXCOLOR color )
{
    // ERROR HERE
    font->DrawTextA(sprite, text.c_str(), charCount, rect, format, color); 
}

以及我对上述功能的使用:

if( !font.Create(d3d.GetDevice(), "Impact", 0, 175, 0, 1, false,
    OUT_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE) )
{
    MessageBox(0, "Could not create font.", "Error!", MB_OK | MB_ICONERROR);
}

// later on...

RECT r = {35, 50, 0, 0};
font.Draw(0, "Test", -1, &r, DT_NOCLIP, d3d.GetColorObj(1.0f, 1.0f, 0.0f, 1.0f));

我能错过什么?


'D3DX10CreateFontIndirect' throws 0x8876086C 找不到是什么意思,但是有些google线程是和d3dDevice有关的,所以我猜想一定和它有关。当我有更多信息时会更新。

4

1 回答 1

3

调用D3DX10CreateFontIndirect实际上并不能保证您的指针将被初始化。

经验法则:HRESULT使用初始化指针的 DirectX 函数时始终检查 s:

HRESULT hr = D3DX10CreateFontIndirect(device, &fd, &font);

if(FAILED(hr)){
    //Get the last error, display a message, etc.
    //Eventually propagate the error if the code can't continue 
    //with the font pointer uninitialized.
}

当你的函数返回时E_FAIL,不要再尝试使用指针。参数值很可能是不正确的(在这里,您的设备指针可能为空,或者您的字体描述可能不正确)。

于 2013-05-30T15:04:08.700 回答