PDA

View Full Version : [D3D] Acces violation when game terminates



chronozphere
26-03-2006, 07:02 PM
Hi!

I'm playing a bit with Clooties DX8.1 SDK. I am new to Direct3d, and i'm exploring a bit.
I already know how to create device and render some colored vertices. Now i want to draw a texture on a Vertex quad, and thats were the problems appear.

Every time i exit the program i get an AV at adress 004190FE and a Runtime error 216 at BF7CCF45.

Okay... my code:

//Global vars
var
Mainform: TMainform;
//Direct3d object
D3D: IDirect3d8 = nil;
//Direct3d device
D3DDevice: IDirect3dDevice8 = nil;
VertexBuffer: IDirect3dVertexBuffer8 = nil;
Texture: IDirect3DTexture8 = nil;

............

//Initalize Direct3d
function InitD3D(hWnd: HWND): ErrorCode;
var
d3ddm: TD3DDisplayMode;
d3dpp: TD3DPresentParameters;
begin
//Creating the device
//etc etc etc.........
//...........

// Turn off culling, and lighting
D3DDevice.SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
D3DDevice.SetRenderState(D3DRS_LIGHTING, iFalse);
D3DDevice.SetRenderState(D3DRS_ALPHABLENDENABLE, 1);
D3DDevice.SetRenderState(D3DRS_SRCBLEND,D3DBLEND_S RCALPHA);
D3DDevice.SetRenderState(D3DRS_DESTBLEND,D3DBLEND_ INVSRCALPHA);
D3DDevice.SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_MODULATE);
Result:= E_SUCCES;
end;

function InitVertexBuffer;
var Vertices: array [0..4] of CUSTOMVERTEX;
pVertices: pByte;
SrcInfo: PD3DXIMAGE_INFO;
const
Transparant: D3DCOLOR = $000000ff;
begin
Result := E_FAILED;

//Create the texture
if FAILED(D3DXCreateTextureFromFileEx(D3DDevice,'c:/frame.bmp',64,64,1,0,
D3DFMT_A8R8G8B8, D3DPOOL_MANAGED, D3DX_FILTER_NONE,
D3DX_DEFAULT, transparant, srcInfo, nil, Texture)) then
begin
Result := E_TEXTURE_CANNOTMAKE;
Exit;
end;

//Initalizing Vertex array
//etc etc....
//....

//create vertex buffer
if FAILED(D3DDevice.CreateVertexBuffer(4*SizeOf(CUSTO MVERTEX),
0, D3DFVF_CUSTOMVERTEX,
D3DPOOL_DEFAULT, VertexBuffer)) then
begin
Result:= E_VB_CREATE_FAILED;
Exit;
end;

//lock vertex buffer
if FAILED(VertexBuffer.Lock(0, SizeOf(Vertices), pVertices, 0)) then
begin
Result:= E_VB_LOCK_FAILED;
Exit;
end;

Move(Vertices, pVertices^, SizeOf(Vertices));

VertexBuffer.Unlock;

Result:= E_SUCCES;
end;

//Release all direct3d objects
procedure Release3d;
begin
if (Texture <> nil) then Texture := nil;
if (VertexBuffer <> nil) then VertexBuffer:= nil;
if (D3DDevice <> nil) then D3DDevice:= nil;
if (D3D <> nil) then D3D:= nil;
end;

It might has something to do with the release order, but i am not sure.
My texture is a 64x64 bitmap with 24 bpp.

Does anyone know a solution, or a good textured-quad drawing example???

Thanx in advance... Chronozphere. :)

Edit->> changed code tags form [code] to [pascal]

User137
26-03-2006, 11:50 PM
I didn't see any device releasing or creating calls...

This code could propably be called as a memory leak :wink:
You are disposing of pointers but leaving the objects in memory.
if (Texture <> nil) then Texture := nil;
if (VertexBuffer <> nil) then VertexBuffer:= nil;
if (D3DDevice <> nil) then D3DDevice:= nil;
if (D3D <> nil) then D3D:= nil;

Sly
27-03-2006, 03:12 AM
In Delphi, all you need to do to release a COM object is set the pointer to nil. This automatically calls Release in the background.

chronozphere
27-03-2006, 02:28 PM
Okay.... looks like i found the bug (i think) :D

The parameters i send to the D3DCreateTextureFromFileEx function are wrong.
This is the way it should be called:


if FAILED(D3DXCreateTextureFromFileEx(D3DDevice,'c:/texture.bmp',0,0,0,0,
D3DFMT_A8R8G8B8, D3DPOOL_MANAGED, D3DX_FILTER_NONE,
D3DX_DEFAULT, transparant, nil, nil, Texture)) then
begin
Result := E_TEXTURE_CANNOTMAKE;
Exit;
end;


The Miplevels and Usage parameters must be zero. (in this case)

Thanx Everyone. :D[/pascal]