我一直被這個問題隨機困擾,我似乎無法弄清楚是什麼導致它。我有一個函數聲明大量的變量,指針和數組。然而,在某個時候,如果我聲明一個新變量,我的程序似乎會崩潰。即使它發生崩潰,它似乎也不一致。如果我的函數下面的代碼,如果我將變量b聲明爲int,它會使程序崩潰,但是如果我聲明它爲int b [10],它將不會。C程序在聲明變量時崩潰
我對很多代碼表示歉意,但我想提供整個功能以防萬一我遺失了一些東西。我所指的變量b靠近頂部。
我懷疑這是某種內存問題,但我所看到的似乎並不一致。
static should_inline void decode_image_internal(PictureDecoderData* pdd, FILE* outfile)
{
unsigned int mb_row;
unsigned int mb_col;
unsigned int mb_idx;
unsigned int i;
unsigned int j;
unsigned int b[10];
unsigned char topy[30720];
unsigned char topcb[7680];
unsigned char topcr[7680];
Picture* pic = pdd->pic;
unsigned int bottom_field_flag = pdd->prev_sh->bottom_field_flag; // TODO: remove the use of prev_sh since it really depends on the pdd decoding context.
slice_header_t* slice0 = pic->field_sh[bottom_field_flag][0]; // get a slice header. It is used for variables that are the same for the whole picture
seq_parameter_set_rbsp_t* sps = slice0->sps;
pic_parameter_set_rbsp_t* pps = slice0->pps;
int PicWidthInMbs = sps->PicWidthInMbs;
unsigned int PicWidthY = PicWidthInMbs * 16;
unsigned int PicHeightInMbs = slice0->PicHeightInMbs;
unsigned int PicSizeInMbs = PicWidthInMbs*PicHeightInMbs;
int CurrMbAddr;
MbAttrib* mb_attr = pic->field_data[bottom_field_flag].mb_attr;
MbAttrib* curr_mb_attr;
unsigned int mbc_width = MbWidthC[1];
unsigned int mbc_height = MbHeightC[1];
// unsigned int mbc_size = mbc_width*mbc_height;
unsigned int PicWidthC = PicWidthInMbs * mbc_width;
int clipY = (1<<sps->BitDepthY)-1;
int meanY = 1<<(sps->BitDepthY-1);
int clipC = (1<<sps->BitDepthC)-1;
int meanC = 1<<(sps->BitDepthC-1);
int mb_data_size = (256+2*MbSizeC[1]);
int16_t* mb_data = pic->field_data[bottom_field_flag].data;
int16_t* curr_mb_data;
unsigned int field_pic_flag = slice0->field_pic_flag;
unsigned int strideY = PicWidthY << field_pic_flag;
unsigned int strideC = PicWidthC << field_pic_flag;
slice_header_t* sh;
unsigned int constrained_intra_pred_flag = pps->constrained_intra_pred_flag;
pixel_t* Y;
pixel_t* C[2];
pixel_t* y;
pixel_t* c[2];
Y = (pixel_t*)((uint32_t)pic->Y + (bottom_field_flag!=0)*PicWidthY);
C[0] = (pixel_t*)((uint32_t)pic->C[0]+ (bottom_field_flag!=0)*PicWidthC);
C[1] = (pixel_t*)((uint32_t)pic->C[1]+ (bottom_field_flag!=0)*PicWidthC);
for (j = 0; j<=pic->slice_num[bottom_field_flag]; j++)
{
sh = pic->field_sh[bottom_field_flag][j];
CurrMbAddr = sh->first_mb_in_slice;
//for (i = 0; i<sh->mb_nb; i++)
for (i = 0; i<2; i++)
{
pixel_t ysamp[256], cbsamp[8*8], crsamp[8*8];
mb_row = (CurrMbAddr)/PicWidthInMbs;
mb_col = (CurrMbAddr) % PicWidthInMbs;
mb_idx = (CurrMbAddr);
curr_mb_attr = &mb_attr[mb_idx];
curr_mb_data = mb_data + mb_idx * mb_data_size;
// printf(" %d %d \n ",strideY, mb_row);
y = Y + mb_col*16 + mb_row*strideY*16;
c[0] = C[0] + mb_col*mbc_width + mb_row*strideC*mbc_height;
c[1] = C[1] + mb_col*mbc_width + mb_row*strideC*mbc_height;
{
MB_TYPE mb_type = curr_mb_attr->mb_type;
unsigned int mb_field_decoding_flag = curr_mb_attr->mb_field_decoding_flag;
pixel_t* mb_C_samples[2];
mb_C_samples[0] = c[0];
mb_C_samples[1] = c[1];
//if (mb_type <= SI) // Intra mb
decode_intra_mb(curr_mb_attr, mb_type, curr_mb_data, 1920, 960, y, mb_C_samples[0], mb_C_samples[1],
ysamp, cbsamp, crsamp,
mbc_height, mbc_width, clipY, clipC, meanY, meanC, mb_field_decoding_flag, mb_row&1, PicWidthInMbs, 0,
constrained_intra_pred_flag);
rgbconvert(ysamp, cbsamp, crsamp, outfile);
}
CurrMbAddr = NextMbAddress(sh->MbToSliceGroupMap, CurrMbAddr, PicSizeInMbs, pps->num_slice_groups_minus1);
}
}
// Release the ref picture it was using
release_picture_refpics(pic, bottom_field_flag); // it is OK even when the pic is a frame because both field points to the same lists
filter_image(pic, bottom_field_flag);
// Output the picture !
add_image_to_dpb(pic);
}
任何指導將不勝感激!
編輯*感謝回覆的傢伙!結果發現堆棧的腐敗是正確的答案。我正在努力修復它。
這個症狀總是意味着你的代碼以某種方式覆蓋了堆棧。您需要在調試器(或者更好的是,存儲器違規檢測器,如Valgrind)中運行它,或者通過分析它所做的每個寫操作來確定它失敗的位置。 – unwind 2013-02-22 10:08:08
不是問題的答案,但肯定是很好的指導,請使用Valgrind:http://www.valgrind.org/此處的示例用法:http://www.cs.swarthmore.edu/~newhall/unixhelp/purify.html – nevelis 2013-02-22 10:08:19