崩溃的原因..

Reason for crash..?

本文关键字:崩溃      更新时间:2023-10-16

我对C++没有太多了解,我正在使用其他人开发的文件在我的图像上应用autoLocalThresholding。

这是我调用该方法的方式。

UIImage *newImage = [imageProcessing resizeImage:image];
ImageWrapper *greyScale=Image::createImage(newImage, newImage.size.width, newImage.size.height);
ImageWrapper *edges=greyScale.image->autoLocalThreshold();

这是这些函数的定义。

ImageWrapper* Image::autoLocalThreshold() {
    const int local_size=8;
    // now produce the thresholded image
    uint8_t *result=(uint8_t*) malloc(m_width*m_height);
    // get the initial total
    int total=0;
    for(int y=0; y<local_size; y++) {
        for(int x=0; x<local_size; x++) {
            total+=(*this)[y][x];
        }
    }
    // process the image
    int lastIndex=m_width*m_height-(m_width*local_size/2+local_size/2);
    for(int index=m_width*local_size/2+local_size/2; index<lastIndex; index++) {
        int threshold=total/64;
        if(m_imageData[index]>threshold*0.9)
            result[index]=0;
        else
            result[index]=255;
        // calculate the new total
        for(int index2=index-m_width*local_size/2-local_size/2; index2<index+m_width*local_size/2-local_size/2; index2+=m_width) {
            total-=m_imageData[index2];
            total+=m_imageData[index2+local_size];
        }
    }
    return Image::createImage(result, m_width, m_height, true);
}

现在这给了我这一行一个EXEC_BAD_ACCESS错误。.

            total+=(*this)[y][x];

任何人都可以解释为什么会发生这种情况以及补救措施是什么...... ?开源文件可在此处获得。https://code.google.com/p/simple-iphone-image-processing/source/browse/trunk/Classes/Image.mm?r=15

而且这种情况有时不会每次都发生。提前致谢

对象 newImage 由于 OP 指出的调整大小方法的问题而未实例化。