c# - "Attempted to read or write protected memory."将项目添加到 List<>

标签 c# wpf

偶然发现奇怪的错误。少量研究表明,当您从代码中篡改内存时,会弹出这种错误。我不做这样的事。

完整错误:

An unhandled exception of type 'System.AccessViolationException' occurred in Cineman.ni.DLL

Additional information: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

没有可用的堆栈跟踪。

这是一些代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Cineman.ViewModels
{
    public class AirTime
    {
        public string Time { get; set; }
    }
}

发生错误的代码:

    public List<AirTime> AirTimes { get; set; }

    public async Task<bool> GetDetailsAsync()
    {
        if (this.Year == null)
        {

            this.IsDetailsLoaded = "visible";
            Uri uri = new Uri(this.DetailsUrl);
            HttpClient client = new HttpClient();
            HtmlDocument htmlDocument = new HtmlDocument();
            HtmlNode htmlNode = new HtmlNode(0, htmlDocument, 1);
            MovieData Item = new MovieData();
            string HtmlResult;
            try
            {
                HtmlRequest = await client.GetAsync(uri, _cts.Token);
                HtmlResult = await HtmlRequest.Content.ReadAsStringAsync();
            }
            if (!(HtmlResult == null) && HtmlRequest.IsSuccessStatusCode)
            {
                await this.DownloadPosterAsync();
                htmlDocument.LoadHtml(HtmlResult);
                this.LargePoster = htmlDocument.DocumentNode.SelectSingleNode("//div[@class='cinema_img']/a").GetAttributeValue("href", "No poster image");
                await this.DownloadLargePosterAsync();
                htmlNode = htmlDocument.DocumentNode.SelectSingleNode("//div[@class='sessions_day']/table/tbody");
                if (this.Today)
                {
                    foreach (var child in htmlNode.ChildNodes)
                    {
                        var time = new AirTime();
                        //time.Time = DateTime.Parse(child.SelectSingleNode("td[1]").InnerText);
                        time.Time = child.SelectSingleNode("td[1]").InnerText;

                        this.AirTimes.Add(time); //Error fires up here

                        //this.AirTimeText += child.SelectSingleNode("td[1]").InnerText + "  ";
                    } 
                }
                this.IsDetailsLoaded = "Collapsed";
            }
        }
        return true;
    }

到目前为止我尝试了什么:
1.我把class和property删了,重新做了。运气不好。
2. 我评论了一个抛出错误的代码,一切正常。
3. 我取消选中 JIT 优化。没有帮助。
4. 我检查了所有变量,它们不为空,所有类型转换就位
5. 阅读大量相关文章和帖子:)
6. 我什至重启了电脑,手机,删除了隔离存储,尝试在手机上部署发布包而不是调试。
我没有尝试的是尝试在模拟器上启动应用程序,因为我有 Win 8 并且无法访问我的桌面。

最佳答案

这不是公共(public)属性(property)吗?

 public List<AirTime> AirTimes { get; set; }

创建一个类变量并实例化它:

private List<AirTime> mobjAirTimes = new List<AirTime>;

然后从您的属性(property)中归还它。

public List<AirTime> AirTimes
{
    get 
    {
       return mobjAirTimes ; 
    }
    set 
    {
       mobjAirTimes = value; 
    }
}

关于c# - "Attempted to read or write protected memory."将项目添加到 List<>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22076423/

相关文章:

c# - 在异步任务完成之前返回

c# - WPF DataGrid - 数据绑定(bind)列到页面的 ViewModel

wpf - ListBox MVVM 中的 ClearSelection

wpf - DevExpress Barbuttonitem与caliburn micro一起

c# - Observable.Interval 对高频事件有用吗?

c# - LINQ 组一种类型的项目

C# 数据集、分页和大量数据

c# - Treeview ContainerFromItem 总是返回 null

wpf - 是否可以只渲染 GradientBrush 的一定百分比?

wpf - 在 WPF 中创建填充图案