Archived

This topic is now archived and is closed to further replies.

Sfpiano

Lock VB access violation

Recommended Posts

g_pD3DDevice->CreateVertexBuffer(4*sizeof(CUSTOMVERTEX), D3DUSAGE_WRITEONLY, D3DFVF_CUSTOMVERTEX,D3DPOOL_DEFAULT, &pSB_VB, NULL); 
Then I pass a pointer to an array called stream data.
static VOID* pVertices;
pSB_VB->Lock(0, sizeof(*streamData), (void**)&pVertices, 0);
 
And it gives me an access violation on the lock call

Share this post


Link to post
Share on other sites
Ok, this could just be that I'm at work and without my DirectX help files, but wouldn't you want to put D3DUSAGE_DYNAMIC in the CreateVertexBuffer() call instead of D3DUSAGE_WRITEONLY, which you would put in the Lock() call? And as was said, you should always have something like this:

if( FAILED(hr = pDevice->...) )
{
// there is a D3DX function to convert HRESULT -> ASCII

// can't remember it though...

cout << "Couldn't Create Vertex Buffer: " << theError << endl;
}


EDIT: some symantical errors

Chris Pergrossi
My Realm | "Good Morning, Dave"

[edited by - c t o a n on July 29, 2003 1:10:43 AM]

Share this post


Link to post
Share on other sites
I actually do have an error check on that, and it''s working fine, thanks though. I tried passing 4*sizeof(CUSTOMVERTEX), but that didn''t work either, nor did switching in the dynamic and writeonly flags. The wierd thing is that the Lock call isn''t returning an error through HRESULT, but my program crashes as soon as it gets to that line.

Share this post


Link to post
Share on other sites
1) When it gives you the access violation, go into where it''s stopped in the debugger and move the mouse pointer over the "pSB_VB" part of that line. What''s it''s value? - the buffer pointer may have become trashed.

2) sizeof() is a compile-time operator... so sizeof(*streamData) isn''t going to read from the streamData pointer points to - instead it''ll just tell you the size of the pointer.

3) Also why is the pVertices pointer declared as static? - I hope you''re not planning on locking the buffer once and using the pointer that''s been returned on subsequent calls. Very Bad Idea if you are.

--
Simon O''Connor
ex -Creative Asylum
Programmer &
Microsoft MVP

Share this post


Link to post
Share on other sites
1) 0xcdcdcdcd

2)Then how would I pass an array of vertices into the lock function?

3)What exactly is pVertices? I''ve only had it because it has to be there, and I thought not constantly destroying and creating a new one would save some time.

Share this post


Link to post
Share on other sites
1) - that''s not a valid pointer. That''s your problem. 0xCDCDCDCD is the value uninitialised variables are set to in debug builds.

So either:
- you''ve not assigned a anything to pSB_VB by the time that crashes,

- the CreateVertexBuffer() call has failed [the debug runtime WILL tell you why] or

- something has trashed the memory where your pointer is

--
Simon O''Connor
ex -Creative Asylum
Programmer &
Microsoft MVP

Share this post


Link to post
Share on other sites
But isn''t the Lock call where you assign data to the VB? I ran it again, and I''m not getting any errors from the CreateVB function. Do you have any idea what could be messing with the memory there?

Share this post


Link to post
Share on other sites
the VB itself isn''t an object yet, Lock only gives you a pointer to the memory allocated for the VB in CreateVertexBuffer.

put a breakpoint on the CreateVertexBuffer line, press F10, and see what the pSB_VB is after that. if its still 0xCDCDCDCD, CreateVB failed.

Share this post


Link to post
Share on other sites
Can you post some more complete code. In particular the complete path between the CreateVertexBuffer() call that should be filling in the pSB_VB pointer and the Lock call that uses that pointer. Because at the point of the crash, the pSB_VB pointer is uninitialised - which firmly indicates either a memory trash, CreateVertexBuffer() failing or CreateVertexBuffer() not actually being called at all!.

Try putting breakpoints on 1) the Lock() call and 2) the CreateVertexBuffer() calls. See which one triggers first (if it''s not the CreateVertexBuffer() one, something is wrong with your program flow control). Try single stepping the code in between the two, keeping a watch on pSB_VB

--
Simon O''Connor
ex -Creative Asylum
Programmer &
Microsoft MVP

Share this post


Link to post
Share on other sites
Just a word of advice: be sure to check the ''this'' pointer when you create the buffer and when you try to access it. I know from my experience that sometimes the this pointer would get accidentally trashed (not totally, maybe just some random bit) and then whatever pointers are in the class APPEAR to point to uninitiated memory. And I don''t think something overwrote his vertex buffer pointer, as for the value to correspond to 0xCD many times over appears a bit coincidental (and I never knew 0xCDCDCDCD was RESERVED for uninitiated variables! I just knew that my variables sometimes took on that value....cool!)

Chris Pergrossi
My Realm | "Good Morning, Dave"

Share this post


Link to post
Share on other sites
The problem was never in the CreateVB call, something was getting changed down the line. I changed a couple of things around, and it no longer crashes, but now it doesn''t draw anything. I believe it''s due to the way I''m passing my vertices to the Lock call.

Share this post


Link to post
Share on other sites