运行在Visual Studio中托管的调试应用程序和直接运行它之间有什么区别? [英] What's the difference between running a debug app hosted in Visual Studio and running it directly?

查看:339
本文介绍了运行在Visual Studio中托管的调试应用程序和直接运行它之间有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个运行在.Net框架4上的应用程序,我的应用程序运行托管和非托管代码。 UDP套接字与自定义通讯堆栈一起使用,与我们的定制硬件交谈。当应用程序从Visual Studio运行时,一切都很好,但当它自己运行时,它经常冻结。我已经看到在Windows XP SP3和Windows 7 SP1的行为。当应用程序被冻结,我可以看到它被卡在ntdll.dll在我的大多数线程。我在另一个问题线程告诉这是正常的,死锁仍然是由我的代码。



这是很难调试,因为我只能附加到应用程序和暂停在本机模式,所以我看到的是反汇编。我在寻找任何可以帮助我的提示。所以,有没有人有一个想法什么可能导致应用程序冻结时运行直接或当从Visual Studio使用F5运行时?我读的地方,在托管代码,GC行为不同,有什么区别?此外,对于非托管代码,我收集了内存初始化不同。再次有什么区别?



任何帮助都非常感谢!



Seb

解决方案

在没有调试器的情况下运行Release版本时,不同的是JIT优化器被启用。代码运行速度更快。其中与您的问题有很强的相关性,线程问题是时序敏感的。计算自己的幸运(我相信你不),这是错误的,而你的代码仍然在你的开发机上运行,​​真正令人讨厌的线程问题是每周一次在你的客户的机器上弄乱你的程序。 p>

使这个问题可调试仍然是解决它的最重要的方法。除了工具+附加到进程,另一种方式来获得调试器启动是通过使用System.Diagnostics.Debugger.Launch()在代码中。你会得到一个对话框,让你选择调试器。



另一种不需要附加调试器的方法是将VS切换到Release版本。使用工具+选项,调试,常规和untick禁止JIT优化模块负载。按F5现在可以在启用优化器和预先附加调试器的情况下运行程序。仍然不是一个slamdunk,因为附加调试器本身会导致时序差异。



另一个常见的方法来解决困难的并发错误是添加日志到您的代码。每当它获得一个锁,写一行。运气,你会很快找到死锁的原因。如果你不那么幸运,日志记录会改变线程的时间,使得死锁消失。



代码审查是另一种方法,特别是当某人执行时其他。最后但并非最不重要的是,考虑线程是否真的是你的应用程序中的最佳解决方案。 The叨我真的解决了吗?感觉永远不会消失,证明线程代码没有缺陷是接近不可能的。


I have an application running on the .Net framework 4 and my application runs managed AND unmanaged code. UDP sockets are used with a custom comms stack to speak with our custom hardware. When the application is run from Visual Studio, all is fine, but when it is run on its own, it often freezes. I have seen the behavior on both Windows XP SP3 and Windows 7 SP1. When the app is frozen, I can see that it is stuck in ntdll.dll in most of my threads. I was told in another question thread that this is normal, that the deadlock is still caused by my code.

This is very hard to debug, because I can only attach to the app and pause it in Native mode, so all I see is disassembly. I'm looking for any hint that could help me. So, does anyone have an idea what could cause an application to freeze when run directly vs. when run from Visual Studio using F5 ? I read somewhere that, in managed code, the GC acts differently, what are the differences ? Also, for unmanaged code, I gathered that memory is initialized differently. Again what is the difference ?

Any help is greatly appreciated !

Seb

解决方案

The one thing that's different when running the Release build without a debugger is that the JIT optimizer is enabled. The code runs faster. Which has a strong correlation with your problem, threading problems are timing sensitive. Count yourself lucky (I'm sure you don't) that this goes wrong while your code is still running on your dev machine, the really nasty threading problems are the ones mess up your program once a week on your customer's machines.

Making this problem debuggable is still the most important way to tackle it. Other than Tools + Attach to Process, another way to get the debugger started is by using System.Diagnostics.Debugger.Launch() in your code. You'll get a dialog that lets you choose the debugger.

Another way that doesn't require attaching the debugger is to switch VS to the Release build. Use Tools + Options, Debugging, General and untick "Suppress JIT optimization on module load". Pressing F5 now runs your program with the optimizer enabled and the debugger pre-attached. Still not quite a slamdunk since having the debugger attached in itself causes timing differences.

Another common approach to tackle difficult concurrency bugs is to add logging to your code. Write a line whenever it acquired a lock. With luck, you'll quickly find the deadlock reason. If you're not so lucky, the logging alters the thread timing enough to make the deadlock disappear. There have been plenty of programs shipped with logging left enabled because that was the only way to keep them running.

A code review is yet another approach, especially when done by somebody else. And last but not least, consider whether threading is really the optimal solution in your app. The nagging "did I really solve it?" feeling never goes away, proving that threaded code is free of defects is close to impossible.

这篇关于运行在Visual Studio中托管的调试应用程序和直接运行它之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆