But we didn't allocate any memory here. What I understand is that after the call to glMapBuffer(), mappedBuf should be treated as GPU memory, so it's not up to us to free it. We copy the texture into this mapped memory, then call glUnmapBuffer() to indicate to OpenGL that we're done copying/modifying that buffer (the buffer that belongs to the driver, not us).
Any pointer in your code resides in CPU memory. The data contained within may come from GPU memory, but the pointer still needs to be declared in your code.
This function doesn't make any sense:
void mapAndCopyToBuffer(char* img1)
{
glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, pixelbufferHandle);
mappedBuf = (char*) glMapBuffer(GL_PIXEL_UNPACK_BUFFER, GL_WRITE_ONLY);
memcpy(mappedBuf, img1, w * h * s);
glUnmapBuffer(GL_PIXEL_UNPACK_BUFFER);
mappedBuf = NULL;
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, pixelbufferHandle);
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, w, h, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, 0);
}The memcpy is copying img1 into mappedBuf and then you are setting mappedBuf to NULL. I suspect that you meant: memcpy(img1,mappedBuf,w * h * s);?
Also, since you know that img1 and mappedBuf will be w * h * s bytes, and that mappedBuf is only used in this function, why not do this:
void mapAndCopyToBuffer(char* img1)
{
char *mappedBuf;
glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, pixelbufferHandle);
mappedBuf = new char[w * h * s];
mappedBuf = (char*) glMapBuffer(GL_PIXEL_UNPACK_BUFFER, GL_WRITE_ONLY);
memcpy(img1, mappedBuf, w * h * s);
glUnmapBuffer(GL_PIXEL_UNPACK_BUFFER);
delete [] mappedBuf;
mappedBuf = NULL;
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, pixelbufferHandle);
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, w, h, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, 0);
}