Answer the question
In order to leave comments, you need to log in
An AccessViolationException error appears: "An attempt was made to read or write to protected memory." What could be the problem?
What is the point: it was necessary to make a drop-down list of users (combobox). After a user is selected from the list, he is added to a certain file, then the combobox'a field is again free for input.
Implementation: In the field combobox'a begin to enter the full name, a drop-down list appears with users whose full name matches the entered. The search for matches is implemented through a request to the server API (using JIRA), then the resulting list is processed and stored in an array, then this list is displayed in the combobox.
Problem: sometimes the code works stably, but it happens that after every 4-5 requests an AccessViolationException error appears "An attempt was made to read or write to protected memory. This often indicates that other memory is corrupted."
I would like to fix this problem. Previously, the error appeared more often, I thought that the server was blocking frequent calls, so the request was sent every 4 characters. After that, the problem was forgotten until the program was used in normal operating mode.
public void dropDownList(ComboBox comboBox)
{
comboBox.Text = comboBox.Text;
string text = comboBox.Text;
string url = "https://serverAddress/api/2/user/picker?maxResults=7&query=" + text;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.CookieContainer = new CookieContainer();
request.Method = "GET";
request.ContentType = "application/json; charset=utf-8";
request.CookieContainer.Add(Session.cookies);
try
{
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
Stream stream = response.GetResponseStream();
JsonSerializer jsonSerializer = new JsonSerializer();
using (StreamReader reader = new StreamReader(stream))
{
using (JsonTextReader jsonTextReader = new JsonTextReader(reader))
{
var obj = jsonSerializer.Deserialize(jsonTextReader);
string jsonString = @JsonConvert.SerializeObject(obj);
JObject jObject = JObject.Parse(jsonString);
JArray array = (JArray)jObject["users"];
comboBox.Items.Clear();
comboBox.SelectionStart = text.Length;
int count = array.Count;
userArray = new string[count];
string temp = "";
for (int i = 0; i < count; i++)
{
temp = string.Format("{0}", array[i]["html"]);
temp = temp.Replace("strong>", "").Replace("<", "").Replace("/", "");
userArray[i] = temp;
temp = "";
}
comboBox.Items.AddRange(userArray);
Array.Clear(userArray, 0, count);
}
}
}
}
catch
{
MessageBox.Show("Что-то пошло не так");
}
}
Answer the question
In order to leave comments, you need to log in
This error can only occur in unmanaged code.
PS: what version of the framework? In 4th SEH exceptions are not caught by default.
1. In "AssemblyInfo.cs" you need to add:
2. Instead of:
catch
{
MessageBox.Show("Что-то пошло не так");
}
catch (Exception ex)
{
Debug.WriteLine(ex);
MessageBox.Show(ex.Message);
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question