corrupted stack
<p>This is my function and it says </p>

<blockquote>
  <p>stack around variable cinfo was corrupted. </p>
</blockquote>

<p>and it says that problem is in line 551 which is last line in this posted code.</p>

<pre><code>struct jpeg_compress_struct cinfo;
struct jpeg_error_mgr       jerr;

FILE*      pOutFile;     //Target file 
int        nSampsPerRow; //Physical row width in image buffer 
JSAMPARRAY jsmpArray;    //Pixel RGB buffer for JPEG file

cinfo.err = jpeg_std_error(&amp;jerr); //Use default error handling (ugly!)

jpeg_create_compress(&amp;cinfo);

if ((pOutFile = fopen(csJpeg, "wb")) == NULL)
{
    *pcsMsg = "Cannot open ";
    *pcsMsg += csJpeg;
    jpeg_destroy_compress(&amp;cinfo);
    return FALSE;
}

jpeg_stdio_dest(&amp;cinfo, pOutFile);

LPBITMAPINFOHEADER lpbi = (LPBITMAPINFOHEADER)hDib;

cinfo.image_width      = lpbi-&gt;biWidth;  //Image width and height, in pixels 
cinfo.image_height     = lpbi-&gt;biHeight;
cinfo.input_components = 3;              //Color components per pixel
                                         //(RGB_PIXELSIZE - see jmorecfg.h)
cinfo.in_color_space   = JCS_RGB;        //Colorspace of input image

jpeg_set_defaults(&amp;cinfo);

jpeg_set_quality(&amp;cinfo,
                 nQuality, //Quality: 0-100 scale
                 TRUE);    //Limit to baseline-JPEG values

jpeg_start_compress(&amp;cinfo, TRUE);

//JSAMPLEs per row in output buffer
nSampsPerRow = cinfo.image_width * cinfo.input_components; 

//Allocate array of pixel RGB values
jsmpArray = (*cinfo.mem-&gt;alloc_sarray)
            ((j_common_ptr) &amp;cinfo,
             JPOOL_IMAGE,
             nSampsPerRow,
             cinfo.image_height);

if (DibToSamps(hDib,
               nSampsPerRow,
               cinfo,
               jsmpArray,
               pcsMsg))
{
    //Write the array of scan lines to the JPEG file
    (void)jpeg_write_scanlines(&amp;cinfo,
                               jsmpArray,
                               cinfo.image_height);
}

jpeg_finish_compress(&amp;cinfo); //Always finish

fclose(pOutFile);

jpeg_destroy_compress(&amp;cinfo); //Free resources

if (*pcsMsg != "")
    return FALSE;

else
    return TRUE;
}
</code></pre>

<p>stack trace:>   WindowBitmap.exe!CBitmapFile::JpegFromDib(void * hDib, int nQuality, ATL::CStringT > > csJpeg, ATL::CStringT > > * pcsMsg)  Line 551 + 0x28 bytes C++</p>

<p>error message: Run-Time Check Failure #2 - Stack around the variable 'cinfo' was corrupted.
pcsMsg definition:
     CString*   pcsMsg </p>
