2012-08-24 179 views
0

这是我的功能和它说,围绕可变CINFO损坏的堆栈

堆栈已损坏。

它说这个问题在551行是最后一行在这个发布的代码。

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(&jerr); //Use default error handling (ugly!) 

jpeg_create_compress(&cinfo); 

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

jpeg_stdio_dest(&cinfo, pOutFile); 

LPBITMAPINFOHEADER lpbi = (LPBITMAPINFOHEADER)hDib; 

cinfo.image_width  = lpbi->biWidth; //Image width and height, in pixels 
cinfo.image_height  = lpbi->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(&cinfo); 

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

jpeg_start_compress(&cinfo, TRUE); 

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

//Allocate array of pixel RGB values 
jsmpArray = (*cinfo.mem->alloc_sarray) 
      ((j_common_ptr) &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(&cinfo, 
           jsmpArray, 
           cinfo.image_height); 
} 

jpeg_finish_compress(&cinfo); //Always finish 

fclose(pOutFile); 

jpeg_destroy_compress(&cinfo); //Free resources 

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

else 
    return TRUE; 
} 

堆栈跟踪:> WindowBitmap.exe CBitmapFile :: JpegFromDib(无效* hDib,INT nQuality,ATL :: CStringT>> csJpeg,ATL :: CStringT>> * pcsMsg)线551 + 0×28字节C++

错误消息:运行时检查失败#2 - 变量'cinfo'周围的堆栈已损坏。 pcsMsg定义: CString的* pcsMsg

+0

你能发布堆栈跟踪和错误消息吗? – noisecapella

+0

a)什么是'它'告诉你你的堆栈已损坏。 b)你的参数是什么? pcsMsg的使用看起来很奇怪 –

+0

您能否提供'pcsMsg'的定义和函数原型。您对设置pcsMsg的使用看起来不正确 –

回答

0
jpeg_destroy_compress(&cinfo); 

释放自动变量,其中被释放的第二时间时cinfo超出范围的存储器中。

函数jpeg_destroy_compress()应释放并释放与压缩对象关联的所有内存。