glReadPixels crash

I’m not sure why this is happening, but if I try to do a glReadPixels with a large screen, my program dies horribly inside OpenGL (Win32). If I cut back the size to something smaller it works fine (say 640x480). The size I’m trying to do is 1280x1024. The malloc on the pixels is working fine (I tried new and a couple of other things, but Windows is fine giving me the 1.5mb of memory I ask for). It’s just glReadPixels fails if the size is too big. Anyone have any ideas? Maybe there’s something extra I have to setup here before I do my screen capture? Here’s the code I’m using, pretty straight forward:

// Get the pixels from the screen
// 640x480 works but large values like
// 1280x1024 dont
int w = m_ClientRect.right;
int h = m_ClientRect.bottom;
int imageSize = 3 * w * h;

unsigned char *pPixelData = (unsigned char *)malloc(imageSize);

if(pPixelData != NULL)
// Copy from OpenGL
glReadPixels(0, 0, w, h, GL_RGB, GL_UNSIGNED_BYTE, pPixelData);

// Create and save the image

// Cleanup



Well i can tell you your asking for almost 4 megs of memory not 1.5.
What Error message does it give you? i cant help without more specifics.

Thanks. I figured it out by searching for another topic on glReadPixels and trying it out. Turns out that when I would use a standard size (hard coded) like 640x480 or even 1280x1024 it would work. When I used the sizes from my application, it usually crashed. I put in calls to glPixelStorei to set the alignment to 1 byte and it worked fine. Guess when I was doing it with the application window sizes, the pixels weren’t aligned on the right byte boundary. Thanks.