In my application (Delphi 2010, OpenGL, windows XP), I need to read back the pixels of variable portions of the framebuffer.
The area of interest is input by the user through a selection rectangle (x1, y1, x2, y2).
With this coordinates I do this:
var
pixels : PGLUByte; //pointer to unsigned bytes
begin
[Transformation of coordinates to Opengl viewport offsets]
//reserve a block of memory for readpixels to write to
ReallocMem(pixels, width * height* sizeof(GLUByte)*3); //<<< crash on this line after a few iterations
if not assigned(pixels) then exit;
//read the pixels
glReadPixels(startx, viewport[3] - (starty+height),
width , height,
GL_RGB, GL_UNSIGNED_BYTE,
pixels);
//Processing of the pixel data follows here...
//when done, release the memory
ReallocMem(pixels, 0);
end;
This function seems to work as intended at the first few tries, but after a few calls to it, the application crashes with an Access violation at $0000000 on the first ReallocMem.
I tried using Getmem, Finalize and Freemem, but these functions lead to the same behaviour.
Is my design principially correct? I tried to debug it, but i could not identify the cause of trouble. width
and height
always have plausible values, and allocating 5-10 blocks of 30 to 120 KiB should not be an issue on a machine with 3 GB of RAM.
Update
Between the calls to this function, The render pipeline might Draw a few frames, objects may be added to the scene - in principle anything the application is capable of, as this function is called when the user decided to select a rectangular portion of my scene for capture through dragging a seelction box over my Canvas.
Here is a sample of widths and heights from a debug session of mine
- width : 211 height: 484 size: 306372
- width : 162 height: 395 size: 191970
- width : 123 height: 275 size: 101475
- width : 14 height: 346 size: 14532
The fourth Selection failed in this session. in Other session, more succesive selections were possible, others crashed when trying the second, but none on the first.
Another thing: when I comment out glReadPixels, no more crashes appear.