Thread: Access Violation Of Memory Buffer WHY??

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #17
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Perhaps the buffer is not lockable. If this is the back buffer then you must set correct flags for the buffer to be locked. Otherwise Lock() is guaranteed to fail.

    Also most surfaces are not lockable. The whole premise behind Direct3D is to eliminate direct accesses to the buffer. Basically you would use DrawPrimitive() to render textures instead of rendering yourself. Also the texture stages are used to do multi-texturing and other effects, but notice you never really get to physically 'touch' the surface in your code.

    I've also done some assembly programming with DirectX buffers and I can tell you that they are not as they appear. For instance:

    Code:
    ...
    bufferoffet=0;
    for (int i=0;i<height;i++)
    {
    Buffer.ptrBuffer[bufferoffset]=color_value;
    bufferoffset+=Buffer.Pitch;
    }
    ...
    This works in C/C++ but not in assembly, barring the Buffer structure here in assembly. Assume that ptrBuffer is a pointer to a valid Direct3D buffer that has been locked.


    Code:
    ...
    START:
    mov edi,ptrBuffer
    xor	edx,edx
     
    RENDER: 
    mov eax,colorvalue
    stosd
    ;account for DWORD we just wrote
    sub edi,4
     
    ;move down one line
    add edi,BufferPitch
     
    ;increment height counter
    inc edx
     
    ;test for end of loop condition
    cmp edx,0
     
    ;if edx!=0 (ZF=0) then short jump to RENDER label
    jne RENDER
    ...
    ...
    But this does not work. You will get several small images if you do this. My assumption was since you are using Buffer.Pitch or BufferPitch as the width of the buffer then it must already be shifted left by the correct value to account for writing DWORDs and not BYTEs. However, this is not the case. In order for the code to work, barring any other errors, you must

    Code:
    ...
    ;use ebx which hasn't been used yet
    mov ebx,BufferPitch
     
    ;multiply by 4 -> hence 4 bytes per DWORD..BufferPitch doesn't account for this?????
    shl	 ebx,2
    add	edi,ebx
    ..
    But if you multiply BufferPitch by 4 in C you will get a very nast mess on screen and more than likely crash hard.
    Last edited by VirtualAce; 08-12-2004 at 07:59 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Access violation... can't figure it out...
    By Raigne in forum C++ Programming
    Replies: 7
    Last Post: 10-11-2007, 10:52 AM
  2. access violation in int array
    By George2 in forum C Programming
    Replies: 2
    Last Post: 08-02-2007, 11:28 PM
  3. Access violation when reading a string.
    By Desolation in forum C++ Programming
    Replies: 16
    Last Post: 05-01-2007, 10:25 AM
  4. how to access memory
    By zhu_dave in forum Tech Board
    Replies: 7
    Last Post: 01-10-2007, 06:57 AM
  5. Strange access violation
    By jimmy_anttila in forum Windows Programming
    Replies: 2
    Last Post: 04-11-2004, 03:10 AM