Marshal.Copy()导致System.StackOverflowException

时间:2014-07-31 19:39:43

标签: c# copy marshalling stack-overflow

我在尝试Marshal.Copy()

时遇到了 System.StackOverflowException 问题

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

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 个答案:

答案 0 :(得分:1)

我追查了它,最终,如果returnArray不是newed大到足以引起这个问题。