在WPF中跟踪AccessViolationException
我已经编写了一个WPF应用程序,它使用许多帧控件来查看摄影机提要。部署时,它会随机崩溃(从2小时到16个多小时),我会在事件日志中连续看到这些: System.AccessViolationException: 试图读取或写入受保护的数据 记忆。这通常是一个迹象 另一个内存已损坏。在 MS.Win32.UnsafentiveMethods.DispatchMessage(MSG& 味精)在 System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame 帧)在 System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame 帧)在 System.Windows.Threading.Dispatcher.Run() 在 System.Windows.Application.RunDispatcher(对象 忽略)在 System.Windows.Application.RunInternal(窗口 窗口)在 System.Windows.Application.Run(窗口 窗口)在 System.Windows.Application.Run()位于 Status\u Station\u client.MainClass.Main() 故障应用状态站 client.exe,版本1.0.0.0,戳记 4ad0faa5,故障模块msvfw32.dll, 版本5.1.2600.2180,邮票41109753, 调试?0,故障地址0x00002642 关于如何追踪这件事有什么想法吗?网页中确实包含ActiveX控件,所以第一个猜测是有问题 我无法在调试模式下跟踪此项。我想尝试的另一件事是接受导航调用的异常,但我不确定这样做是否明智:在WPF中跟踪AccessViolationException,wpf,webbrowser-control,access-violation,Wpf,Webbrowser Control,Access Violation,我已经编写了一个WPF应用程序,它使用许多帧控件来查看摄影机提要。部署时,它会随机崩溃(从2小时到16个多小时),我会在事件日志中连续看到这些: System.AccessViolationException: 试图读取或写入受保护的数据 记忆。这通常是一个迹象 另一个内存已损坏。在 MS.Win32.UnsafentiveMethods.DispatchMessage(MSG& 味精)在 System.Windows.Threading.Dispatcher.PushFrameImpl(Dis
try
{
if (Frame1 != null)
Frame1.Source = new Uri(uriWithResolution);
}
catch (AccessViolationException ex)
{
// log message
}
编辑:这里有更多的源代码,我很难确定错误在哪里(即抛出异常的位置)
MatrixView.cs:
public partial class MatrixView : Window
{
System.Timers.Timer timer;
int pageNumber = 0;
IEnumerable<List<CameraInfo>> _cameraList;
GlobalSettings _globalSettings;
Screen _screen;
public MatrixView(List<CameraInfo> cameras, int pageFlipInterval, int camerasPerPage, GlobalSettings globalSettings, Screen screen)
{
InitializeComponent();
_globalSettings = globalSettings;
_screen = screen;
_cameraList = Partition<CameraInfo>(cameras, camerasPerPage);
this.Dispatcher.UnhandledException += new DispatcherUnhandledExceptionEventHandler(Dispatcher_UnhandledException);
displayCameras();
timer = new System.Timers.Timer(pageFlipInterval * 1000); // interval (in seconds) * 1000 ms / s
timer.Elapsed += new ElapsedEventHandler(timer_Elapsed);
timer.Enabled = true;
this.KeyUp += new System.Windows.Input.KeyEventHandler(MatrixView_KeyUp);
if (globalSettings.FullScreenOnLoad)
{
this.WindowStyle = WindowStyle.None;
}
}
void MatrixView_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
{
if (this.WindowStyle == WindowStyle.None)
{
if (e.Key == Key.F11 || e.Key == Key.Escape)
{
this.WindowStyle = WindowStyle.SingleBorderWindow;
}
}
else
{
if (e.Key == Key.F11)
{
this.WindowStyle = WindowStyle.None;
}
}
this.WindowState = WindowState.Maximized;
}
void timer_Elapsed(object sender, ElapsedEventArgs e)
{
this.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new ThreadStart(delegate()
{
displayCameras();
}));
}
void Dispatcher_UnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
{
EventLog.WriteEntry("Matrix Monitor", string.Format("Unhandled exception from Matrix Dispatcher\r\nMessage: {0}\r\nSource: {1}\r\nInnerException: {2}\r\nStack Trace: {3}\r\nFull String: {4}", e.Exception.Message, e.Exception.Source, e.Exception.InnerException, e.Exception.StackTrace, e.Exception.ToString()));
e.Handled = true;
}
private void displayCameras()
{
foreach (var child in uniformGrid1.Children)
{
FrameTimer c = child as FrameTimer;
if (c != null)
{
c.Dispose();
c = null;
}
}
GC.Collect();
GC.WaitForPendingFinalizers();
uniformGrid1.Children.Clear();
List<CameraInfo> camerasInPage = _cameraList.ElementAt(pageNumber);
int numCameras = camerasInPage.Count;
int sqrtOfCameras = (int) Math.Sqrt(numCameras);
double height = _screen.Bounds.Height / sqrtOfCameras;
double width = _screen.Bounds.Width / sqrtOfCameras;
foreach (CameraInfo camera in camerasInPage)
{
uniformGrid1.Children.Add(new FrameTimer(camera, _globalSettings, height, width));
}
pageNumber++;
if (pageNumber >= _cameraList.Count<List<CameraInfo>>())
{
pageNumber = 0;
}
}
public static IEnumerable<List<T>> Partition<T>(IList<T> source, int size)
{
int remainder = source.Count % size == 0 ? 0 : 1;
for (int i = 0; i < (source.Count / size) + remainder; i++)
yield return new List<T>(source.Skip(size * i).Take(size));
}
}
如果查看stacktrace底部的故障模块,您将看到msvfw32.dll。这不是WPF使用的DLL,因此我假设它来自正在加载的网页中的某个active-x。我甚至更相信这一点,因为您的代码暗示了一些处理相机/视频的东西,而msvfw32处理视频(它也非常古老!!)。它出现在Dispatcher循环中,因为Dispatcher还处理Win32消息循环,该消息循环最终由所谓的activex使用
另外,尝试检查,也许您可以设置参数Handled=true我没有答案(抱歉),但是关于您从导航调用中吞咽异常的想法,我认为这不起作用,因为(根据堆栈跟踪)导航调用中没有出现异常,但随后在dispatcher循环中。因此,程序将在异常发生之前退出try块。@Jeremiah是的,我认为罪魁祸首也在ActiveX控件中。我在主类、matrixview类和frametimer类中有Dispatcher.UnhandledException。主类是一个似乎捕捉到它的类,我想我可能忘了在那里设置Handled=true,不知道这是否有效:)我对调度器的工作方式有点困惑。一个线程是否处理在同一线程上创建的所有对象的调度程序?Win32消息循环也在同一个线程上?WPF调度程序处理“工作”的托管队列并处理Win32消息循环。Win32消息和.NET委托在根据优先级进行处理时是交叉的。它甚至处理Win32消息泵的原因是为了处理低级事务(即拖动WPF窗口、从操作系统获取鼠标信息等),以及驱动任何Win32组件(如activex)。@Jeremiah I搞错了,app.dispatchernhandledException没有捕获它;只有AppDomain.CurrentDomain.UnhandledException事件可以看到它。您可能需要自己启动应用程序的调度程序。不应该太难。您只需从App.xaml中删除StartUri属性,并在App.xaml.cs中覆盖OnStartup。在那里,尝试创建窗口,执行.Show,然后执行Dispatcher.Run,所有这些都在try/catch块中。如果它抛出错误,请尝试重新运行调度程序。运行
public partial class FrameTimer : UserControl, IDisposable
{
System.Timers.Timer timer;
string _uri;
string _noImageUrl;
bool? _successState = null;
GlobalSettings _globalSettings;
CameraInfo _camera;
Ping ping;
double _height;
double _width;
public FrameTimer(CameraInfo camera, GlobalSettings globalSettings, double height, double width)
{
InitializeComponent();
_noImageUrl = AppDomain.CurrentDomain.BaseDirectory + "noImage.jpg";
_globalSettings = globalSettings;
_camera = camera;
_height = height;
_width = width;
_uri = string.Format("http://{0}:{1}/LiveView.aspx?camera={2}", globalSettings.ServerIPAddress, globalSettings.ServerPort, camera.camName);
this.Dispatcher.UnhandledException += new DispatcherUnhandledExceptionEventHandler(Dispatcher_UnhandledException);
setUrl();
timer = new System.Timers.Timer(_globalSettings.PingInterval * 1000);
timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
timer.Enabled = true;
}
void Dispatcher_UnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
{
EventLog.WriteEntry("Matrix Monitor", string.Format("Unhandled exception from Frame Dispatcher\r\nMessage: {0}\r\nSource: {1}\r\nInnerException: {2}\r\nStack Trace: {3}\r\nFull String: {4}", e.Exception.Message, e.Exception.Source, e.Exception.InnerException, e.Exception.StackTrace, e.Exception.ToString()));
e.Handled = true;
}
void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
this.Dispatcher.BeginInvoke(DispatcherPriority.Send, new ThreadStart(delegate()
{
setUrl();
}));
}
private void setUrl()
{
ping = new Ping();
ping.PingCompleted += new PingCompletedEventHandler(ping_PingCompleted);
videoChecks checks = new videoChecks();
string ipAddressToUse = checks.isIPInternal(_camera.camIP) ? _camera.camIP : _camera.camExtIP;
ping.SendAsync(ipAddressToUse, 1000, null);
}
void ping_PingCompleted(object sender, PingCompletedEventArgs e)
{
try
{
if (e.Reply.Status == IPStatus.Success)
{
if (_successState == null || _successState == false)
{
_successState = true;
string uriWithResolution = string.Format("{0}&res={1}x{2}&header=0", _uri, (int)_width, (int)_height);
if (Frame1 != null)
Frame1.Source = new Uri(uriWithResolution);
}
}
else
{
if (_successState == null || _successState == true)
{
_successState = false;
Image1.Source = new BitmapImage(new Uri(_noImageUrl));
}
}
}
catch (ObjectDisposedException ex)
{
Dispose();
}
finally
{
((IDisposable)sender).Dispose();
}
}
#region IDisposable Members
public void Dispose()
{
if (timer != null)
{
timer.Elapsed -= new System.Timers.ElapsedEventHandler(timer_Elapsed);
timer.Enabled = false;
timer.Dispose();
timer = null;
}
Frame1.Source = null;
if (ping != null)
{
ping.PingCompleted -= new PingCompletedEventHandler(ping_PingCompleted);
((IDisposable)ping).Dispose();
ping = null;
}
}
#endregion
}