【问题标题】:Raspberry Attempted to read or write protected memory. This is often an indication that other memory is corruptRaspberry 试图读取或写入受保护的内存。这通常表明其他内存已损坏
【发布时间】:2016-10-23 08:38:01
【问题描述】:

我有以下代码和 StopWatch 对象,当我尝试重新启动它时,它会抛出此异常:

试图读取或写入受保护的内存。这通常表明其他内存已损坏

代码如下:

public sealed partial class MainPage : Page
    {
        private const int ECHO_PIN = 23;
        private const int TRIGGER_PIN = 18;
        private GpioPin pinEcho;
        private GpioPin pinTrigger;
        private DispatcherTimer timer;
        private Stopwatch sw;

        public MainPage()
        {
            this.InitializeComponent();


            InitGPIO();

            timer = new DispatcherTimer();
            timer.Interval = TimeSpan.FromMilliseconds(400);
            timer.Tick += Timer_Tick;
            if (pinEcho != null && pinTrigger != null)
            {
                timer.Start();
            }


        }

        private async void Timer_Tick(object sender, object e)
        {
            pinTrigger.Write(GpioPinValue.High);
            await Task.Delay(10);
            pinTrigger.Write(GpioPinValue.Low);
            while (pinEcho.Read() == GpioPinValue.Low)
            {
                sw.Restart();

            }

            while (pinEcho.Read() == GpioPinValue.High)
            {
            }
            sw.Stop();

            var elapsed = sw.Elapsed.TotalSeconds;
            var distance = elapsed * 34000;

            distance /= 2;
            distancetb.Text = "Distance: " + distance + " cm";

        }
        private async void InitGPIO()
        {
            var gpio = GpioController.GetDefault();
            if (gpio == null)
            {
                pinEcho = null;
                pinTrigger = null;
                gpioStatus.Text = "no hay controlador GPIO en este dispositivo";
                return;
            }

            pinEcho = gpio.OpenPin(ECHO_PIN);
            pinTrigger = gpio.OpenPin(TRIGGER_PIN);


            pinTrigger.SetDriveMode(GpioPinDriveMode.Output);
            pinEcho.SetDriveMode(GpioPinDriveMode.Input);

            gpioStatus.Text = "controlador GPIO inicializado";

            pinTrigger.Write(GpioPinValue.Low);

            await Task.Delay(100);
        }
    }

【问题讨论】:

  • 准确指出哪一行引发了异常可能会有所帮助。
  • 在windows App.gics : global::System.Diagnostics.Debugger.Break();
  • 它通常指向一个特定的行,你知道是哪一个吗?
  • 当我运行程序时,我发送另一个窗口。全局::System.Diagnostics.Debugger.Break();

标签: c# raspberry-pi windowsiot


【解决方案1】:

在您的代码片段中,我没有看到 Stopwatch sw 的任何初始化。

尝试添加

sw = new Stopwatch();

在您的构造函数中或 Timer_Tick 执行之前的某个位置。

【讨论】:

    猜你喜欢
    • 2011-07-13
    • 2010-10-27
    • 2012-06-27
    • 2011-05-26
    • 2011-03-11
    • 2014-04-30
    • 1970-01-01
    • 1970-01-01
    • 2013-01-23
    相关资源
    最近更新 更多