gpt4 book ai didi

C++ delete 在某些实例上崩溃

转载 作者:行者123 更新时间:2023-11-28 03:20:06 26 4
gpt4 key购买 nike

我有以下 C++ 方法:

void MyClass::FindBottom ( CIppImage& bwImage, FieldRec* obField, int& bottomBorder_top, int& bottomBorder_bottom, int& topBorder_top, int& topBorder_bottom, bool& algoFoundBorder ){

int scanYStart = topBorder_top + obField->getH() - VERTICAL_SCAN_AMOUNT;
int scanYEnd = topBorder_top + obField->getH() + ( VERTICAL_SCAN_AMOUNT * 1.5 );
int scanAmount = scanYEnd - scanYStart;
int xScanEnd = obField->getOX() + obField->getW();

int* histogram = new int[ scanAmount ];
memset ( histogram, 0, (scanAmount) * sizeof(int) );

Ipp8u* bwDataPtr = bwImage.DataPtr();

int bwLineWidth = ( bwImage.Width() + 7 ) / 8;
int n = 0;

for( int y = scanYStart; y <= scanYEnd; y++ ){
for( int x = obField->getOX(); x < xScanEnd; x++ ){
if( ( GETBYTE( bwDataPtr, x, y, bwLineWidth ) ) != 0 && ( GETPIXEL(bwDataPtr, x, y, bwLineWidth ) ) != 0 ){
histogram [ n ]++;
}
}
n++;
}

int numFillPixelsThreshold = (int)(HORIZ_FILL_THRESHOLD * obField->getW());
vector<int> goodRows;
for( int j = 0; j < VERTICAL_SCAN_AMOUNT+VERTICAL_SCAN_AMOUNT+1; j ++ ){
if( histogram[ j ] >= numFillPixelsThreshold ) {
goodRows.push_back( scanYStart + j );
}
}
if( goodRows.size() > 0 ){
bottomBorder_top = goodRows[0];
bottomBorder_bottom = goodRows[goodRows.size()-1];
algoFoundBorder = true;
}else{
bottomBorder_top = obField->getOY() + obField->getH() - 4;
bottomBorder_bottom = obField->getOY() + obField->getH() + 4;
algoFoundBorder = false;
}
delete[] histogram;
histogram = 0;
}

有 1 个特定实例,其中 delete[] 调用使程序崩溃,Visual Studio 返回错误消息:

HEAP CORRUPTION DETECTED: after Normal block (#44325) at 0x01214980 CRT detected that the application wrote to memory after end of heap buffer.

有什么想法吗?

最佳答案

int scanAmount = scanYEnd - scanYStart; 
int n = 0;
for( int y = scanYStart; y <= scanYEnd; y++ ){
n++;
}
ASSERT(n == scanAmount); // failed, because your for loop makes one more step and as a result you corrupt heap

关于C++ delete 在某些实例上崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15703195/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com