Attempted to read or write protected memory. This often indicates that another memory is corrupt.

I really don't understand how this error occurs in 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 causing an error. I will also attach a 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 an entire software error and stops working even if I call them in a new thread :(

The error of this line is: System.Windows.Threading.Dispatcher.Run ();

Also check the screenshot

enter image description here

C # 4.0 WPF

+7
c # wpf access-violation unhandled-exception memory-corruption
Apr 02 2018-12-12T00:
source share
3 answers

You use lambda as a function of flow. This lambda is called a new thread. At the time the thread is actually created, it will look for the argument that you specify, which is the local variable srUrl, but by the time this happens, your function (dispatcherTimer_Tick) has already exited, so srUrl will be in the part of the stack that ( consequently, violation of access rights). An easy fix is ​​to define a variable in the class and add srLoc quickly. A better solution is to pass srLoc as an argument:

 () => { startNewWindow(srUrl); } 

becomes

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

Now the function reference and the correct copy of the string are saved for calling the function, and it does not matter that the original srUrl is out of scope by the time the stream starts. I'm not sure. The factory task allows you to pass an array of arguments. dispatchers usually have an overload for this, so maybe you want your window to take care of this.

Now you do this several times, so you may need to wrap the arguments every time they are passed.

0
Apr 03 2018-12-12T00:
source share

I struggled with this problem with the client, and here is what I found.

We are working on a WPF application that does a lot of threading and desktop processing. This exception suddenly began to appear, and I began to do digging. Finally, I discovered the culprit after about an hour of investigation:

  var worker = new BackgroundWorker(); worker.DoWork += (o, ea) => Dispatcher.BeginInvoke(new Action(() => { //do some heavy processing here, plus UI work, then call another method. //inside that other method, I found this: var thread = new Thread(() => { //do some heavy processing. }) { IsBackground = true }; thread.Start(); })); 

What apparently happened was that the background worker is finishing his work and returning from his execution. However, the thread created inside this background is not processed and returned only to detect that the thread that it was created has already gone out of scope, which raises an AccessViolationException.

To debug this, I would like to pay close attention to where the exception occurs, and carefully examine the call stack, which may or may not be destroyed or lost depending on whether you are inside the thread when the exception is thrown.

+2
Aug 08 '13 at 17:22
source share

I had a similar problem a while ago.

The error occurs because your window goes out of scope and the garbage collector destroys it.

Using ShowDialog() should solve the problem. Note that this will not block other threads, because the window will be modal only in the calling thread.

 private void NewWindowThread<T, P>(Func<P, T> constructor, P param) where T : Window { Thread thread = new Thread(() => { System.Windows.Threading.Dispatcher.Run(); T w = constructor(param); w.ShowDialog(); w.Dispatcher.InvokeShutdown(); }); thread.SetApartmentState(ApartmentState.STA); try { thread.Start(); } catch { // log&handle exceptions } } 
+1
Apr 02 2018-12-12T00:
source share



All Articles