I came across a strange mistake. Few studies have shown that...">

"Attempting to read or write protected memory." when adding an item to the list <>

I came across a strange mistake. Few studies have shown that such errors appear when you change memory from code. I do not do this.

full error:

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

Additional Information: Attempted to read or write protected memory. This often indicates that another memory is corrupted.

Missing stack trace.

Here is the code:

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

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

Code in which the error occurred:

    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. . .
2. , , .
3. JIT. .
4. , ,
5. :)
6. , , , .
, Win 8 .

+4
1

?

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

:

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

.

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

Source: https://habr.com/ru/post/1529262/


All Articles