代码之家  ›  专栏  ›  技术社区  ›  Ono

Marshal.Copy()导致System.StackOverflowException

  •  1
  • Ono  · 技术社区  · 10 年前

    我遇到了 System.StackOverflowException 当我试图 Marshal.Copy()

    以下是发生异常的代码截图:

    enter image description here

    功能如下:

    private static void ImageUpdate(IntPtr returnArray, ref int channels)
            {
                if (_prevTickCount == 0)
                {
                    _sumTickCount = 0;
                }
                else
                {
                    _sumTickCount = _sumTickCount * .75 + (Environment.TickCount - _prevTickCount) * .25;
                }
    
                //only copy to the buffer when pixel data is not being read
                if (_pixelDataReady == false)
                {
                    int width = 0;
                    int height = 0;
    
                    if (false == GetImageDimensions(ref width, ref height))
                    {
                        return;
                    }
    
                    _dataLength = width * height;
    
                    _colorChannels = channels;
    
                    if ((_pixelData == null) || (_pixelData.Length != (_dataLength * _colorChannels)))
                    {
                        _pixelData = new short[_dataLength * _colorChannels];
                        //_pixelDataHistogram = new int[_colorChannels][];
                        _pixelDataHistogram = new int[MAX_CHANNELS][];
    
                        if (_colorChannels == 1)
                        {
                            _pixelDataByte = new byte[_dataLength];
                        }
                        else
                        {
                            _pixelDataByte = new byte[_dataLength * 3];
                        }
    
                        //for (int i = 0; i < _colorChannels; i++)
                        for (int i = 0; i < MAX_CHANNELS; i++)
                        {
                            _pixelDataHistogram[i] = new int[PIXEL_DATA_HISTOGRAM_SIZE];
                        }
                    }
    
                    //2^n  == FULL_RANGE_NORMALIZATION_FACTOR
                    const int SHIFT_VALUE = 6;
                    switch (_colorChannels)
                    {
                        case 1:
                            {
                                Marshal.Copy(returnArray, _pixelData, 0, _dataLength * _colorChannels);
    
                                //factor is derived by taking CAMERA_MAX_INTENSITY_VALUE/256
                                //const double FULL_RANGE_NORMALIZATION_FACTOR = 64.0;
    
                                //clear the histogram
                                for (int i = 0; i < PIXEL_DATA_HISTOGRAM_SIZE; i++)
                                {
                                    _pixelDataHistogram[0][i] = 0;
                                }
    
                                for (int i = 0; i < _dataLength * _colorChannels; i++)
                                {
                                    double valHist;
    
                                    if (_pixelData[i] < 0)
                                    {
                                        valHist = (_pixelData[i] + 32768) >> SHIFT_VALUE;
                                    }
                                    else
                                    {
                                        valHist = (_pixelData[i]) >> SHIFT_VALUE;
                                    }
    
                                    _pixelDataHistogram[0][(byte)valHist]++;
                                }
                            }
                            break;
                        default:
                            {
                                Marshal.Copy(returnArray, _pixelData, 0, _dataLength * _colorChannels);
                            }
                            break;
                    }
    
                    _dataWidth = width;
                    _dataHeight = height;
                    _pixelDataReady = true;
                    ThorLog.Instance.TraceEvent(TraceEventType.Verbose, 1, "ImageUpdate pixeldata updated");
                }
                else
                {
                    ThorLog.Instance.TraceEvent(TraceEventType.Verbose, 1, "ImageUpdate pixeldata not ready");
                }
    
                _prevTickCount = Environment.TickCount;
            }
    

    整个想法是从本地代码复制图像缓冲区。只有当图像大小为4K X 4K时,才会出现此异常,但处理低于此大小的图像时,我不会遇到任何问题。

    我不知道该怎么纠正。有人愿意接受教育吗?谢谢

    1 回复  |  直到 10 年前
        1
  •  1
  •   Ono    10 年前

    我终于找到了它,如果是 returnArray 而不是 newed 大到足以引起这个问题。