使用DirectX的一个运行结束弹出错误的问题


我定义了一个Model类好对数据集中,运行正常,但为什么在关闭窗口的时候会报错。。。
Expression:_BLOCK_TYPE_IS_VALID(pHead->nBlockUse)

错误提示。。。

源码如下。。。

   
  //还有一个Date类对D3D中的操作量进行封装
  
class Data
{
public:
HWND hwnd;
MSG msg;

IDirect3D9* d3d9;
IDirect3DDevice9* device;
D3DPRESENT_PARAMETERS d3dpp;

void Direct3DInit (int windowW, int windowH);
};
void Data::Direct3DInit (int windowW, int windowH)
{
this->d3d9 = Direct3DCreate9(D3D_SDK_VERSION);
d3dpp.BackBufferWidth = windowW;
d3dpp.BackBufferHeight = windowH;
d3dpp.BackBufferFormat = D3DFMT_A8R8G8B8;
d3dpp.BackBufferCount = 1;
d3dpp.MultiSampleType = D3DMULTISAMPLE_NONE;
d3dpp.MultiSampleQuality = 0;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.hDeviceWindow = this->hwnd;
d3dpp.Windowed = true; // fullscreen
d3dpp.EnableAutoDepthStencil = true;
d3dpp.AutoDepthStencilFormat = D3DFMT_D24S8; // depth format
d3dpp.Flags = 0;
d3dpp.FullScreen_RefreshRateInHz = D3DPRESENT_RATE_DEFAULT;
d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;
this->d3d9->CreateDevice (
D3DADAPTER_DEFAULT,
D3DDEVTYPE_HAL,
this->hwnd,
D3DCREATE_HARDWARE_VERTEXPROCESSING,
&this->d3dpp,
&this->device );
}

//其他省略

//这时Model类
class Model
{
public:
Vertex* vertex; //物体顶点
WORD* index; //物体顶点渲染顺序
IDirect3DVertexBuffer9* vb; //顶点缓冲区
IDirect3DIndexBuffer9* ib; //索引缓冲区

Data* data;

int pointNum; //物体顶点个数
int sturfaceNum; //物体三角形面个数

inline void modelInit (int pN, int fN);
inline void drawNow ();

Model (Data* _data)
{
this->data = _data;
this->vertex = NULL;
this->index = NULL;
this->vb = NULL;
this->ib = NULL;
}
~Model ()
{
delete [] this->vertex;
delete [] this->index;
this->vertex = NULL;
this->index = NULL;
}
};

inline void Model::modelInit (int pN, int fN)
{
this->pointNum = pN;
this->sturfaceNum = fN;
this->vertex = new Vertex[pN]();
this->index = new WORD[fN*3]();
this->data->device->CreateVertexBuffer (
pN*sizeof(Vertex),
0,
VertexFVF,
D3DPOOL_MANAGED,
&this->vb,
0 );

this->data->device->CreateIndexBuffer (
fN*3*sizeof(WORD),
D3DUSAGE_WRITEONLY,
D3DFMT_INDEX16,
D3DPOOL_MANAGED,
&this->ib,
0 );
}

inline void Model::drawNow ()
{
this->data->device->SetStreamSource (0, this->vb, 0, sizeof(Vertex) );
this->data->device->SetIndices (this->ib);

this->data->device->DrawIndexedPrimitive (D3DPT_TRIANGLELIST, 0, 0, this->pointNum, 0, this->sturfaceNum);
}

//这里开始
Data dl;
Model mo(&dl);

int WINAPI WinMain (
HINSTANCE hInstance = 0,
HINSTANCE hPreInstance = 0,
PSTR szCmdLine = 0,
int iCmdShow = 0)
{
//窗口建立省略

dl.device->SetRenderState(D3DRS_FILLMODE, D3DFILL_WIREFRAME);
dl.device->SetFVF(VertexFVF);

mo.modelInit (4, 2, 0);

mo.vb->Lock (0, 0, (void**)&mo.vertex, 0);
mo.vertex[0]=Vertex(-1.0f, 1.0f, 0.0f);
mo.vertex[1]=Vertex(1.0f, 1.0f, 0.0f);
mo.vertex[2]=Vertex(1.0f, -1.0f, 0.0f);
mo.vertex[3]=Vertex(-1.0f, -1.0f, 0.0f);
mo.vb->Unlock();

mo.ib->Lock (0, 0, (void**)&mo.index, 0);
mo.index[0]=0;
mo.index[1]=1;
mo.index[2]=2;
mo.index[3]=1;
mo.index[4]=2;
mo.index[5]=3;
mo.ib->Unlock();

while (dl.msg.message != WM_QUIT)
{
if (::PeekMessage (&dl.msg, 0, 0, 0, PM_REMOVE) )
{
TranslateMessage (&dl.msg);
DispatchMessage (&dl.msg);
}
else
{
dl.device->Clear (0, 0, dlData::DL_Clear_Type, dlData::DL_Clear_Color, 1.0f, 0);

dl.device->BeginScene();

mo.drawNow();

dl.device->EndScene();

dl.device->Present (0, 0, 0, 0);
}
}

return dl.msg.wParam;

}

帮忙看看哪儿错了。。。谢谢。。。

directx C++

五仰化四铁 12 years, 8 months ago

应该是释放资源的时候出了问题。
你检查一下你调用delete的地方,多半是因为重复释放同一个资源~

在出错的时候选择重试,可以进入vc调试的.......跟踪一下看看哪里出错。查看调用栈,可以找到你出错的代码。
或者用boundschecker来检查

命莲寺忘却缘起 answered 12 years, 8 months ago

Your Answer