尝试读取或写入受保护的内存.这通常表明其他内存已损坏 [英] Attempted to read or write protected memory. This is often an indication that other memory is corrupt

查看:39
本文介绍了尝试读取或写入受保护的内存.这通常表明其他内存已损坏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的不明白这个错误是如何在这段代码中发生的.请自行检查代码

I really do not understand how is this error happening at this code. Please check the code yourself

    void dispatcherTimer_Tick(object sender, EventArgs e)
{
    string srUrl = lstLocalIndex[irLocalIndex] + lstMainIndex[irMainIndex].Replace("0;","");

    Task.Factory.StartNew(() =>
    {
        startNewWindow(srUrl);
    });

}


    void startNewWindow(string srUrl)
{
    NewWindowThread<TitleWindow, string>(c => new TitleWindow(c), srUrl);
}

现在这段代码是错误发生的地方.我也会附上截图

Now this code is where the error happening. I will also attach screenshot

        private void NewWindowThread<T, P>(Func<P, T> constructor, P param) where T : Window
    {
        Thread thread = new Thread(() =>
        {
            T w = constructor(param);
            w.Show();
            w.Closed += (sender, e) => w.Dispatcher.InvokeShutdown();
            try
            {
                System.Windows.Threading.Dispatcher.Run();
            }
            catch
            {

            }
        });
        thread.SetApartmentState(ApartmentState.STA);
        try
        {
            thread.Start();
        }
        catch
        {

        }
    }

即使我在新线程中调用它们,此错误也会导致整个软件抛出错误并停止工作:(

This error causes whole software throw error and stop working even though i am calling them in new thread :(

这一行抛出错误System.Windows.Threading.Dispatcher.Run();

请检查截图

C# 4.0 WPF

C# 4.0 WPF

推荐答案

您正在使用 lambda 作为线程函数.这个 lambda 在一个新线程上被调用.在在实际创建线程的那一刻,它会查找您提供的参数,它是一个局部变量 srUrl,但是当发生这种情况时,您的函数 (dispatcherTimer_Tick) 已经退出,因此 srUrl 将位于堆栈的一部分中不再正确定义(因此访问冲突).简单的解决方法是在类中定义一个变量并快速填充 srLoc.更合适的解决方案是将 srLoc 作为参数实际传递:

You are using a lambda as a thread function. This lambda is called on a new thread. At the moment the thread is actually created, it will look for the argument you supply, which is a local variable srUrl, but by the time this happens your function (dispatcherTimer_Tick) has already exited, so srUrl will be in a part of the stack that is no longer properly defined (hence the access violation). The easy fix is to define a variable in the class and stuff the srLoc there quickly. A more proper solution is to actually pass the srLoc as argument:

() =>
{
    startNewWindow(srUrl);
}

变成

(Action<string>){x => {startNewWindow(x);},
            new object[] {srUrl}

现在为函数调用保存了函数引用和字符串的正确副本,并且在线程启动时原始 srUrl 超出范围无关紧要.我不确定是否任务工厂允许传递参数数组.调度员通常对此有过载,所以也许你想让你的窗口来处理这个.

Now the function reference and a proper copy of the string are saved for the function call, and it doesn't matter that the original srUrl is out of scope by the time the thread kicks in. I'm not sure whether the task factory allows the argument array to be passed. dispatchers normally have an overload for this, so maybe you want to let your window take care of this.

现在你实际上这样做了几次,所以你可能需要在每次传递参数时包装它们.

Now you actually do this a few times, so you may need to wrap the arguments each time they are passed.

这篇关于尝试读取或写入受保护的内存.这通常表明其他内存已损坏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆