简体   繁体   English

从Windows运行时,C#/ C ++应用程序崩溃,但不是从Visual Studio运行

[英]C# / C++ application crashes when run from Windows, but not from Visual Studio

I'm developing a C# application that calls into an unmanaged (C++) dll. 我正在开发一个调用非托管(C ++)DLL的C#应用​​程序。 I've found a certain user operation that consistently leads to the application crashing when run from Windows Explorer. 我发现某个用户操作从Windows资源管理器运行时始终导致应用程序崩溃。 However, when launched from the Visual Studio debugger, the crash does not occur. 但是,从Visual Studio调试器启动时,不会发生崩溃。 I therefore cannot step into the code at the time of the crash and debug exactly what's going on. 因此,我无法在崩溃时进入代码并完全调试正在发生的事情。

What might cause the binary to crash when run from Explorer, but not from Visual Studio? 什么可能导致二进制文件从资源管理器运行时崩溃,但不是从Visual Studio运行? Note that I'm using a Release build; 请注意,我正在使用Release版本; Debug builds crash neither in Visual Studio nor in Explorer. 调试版本在Visual Studio Explorer中都不会崩溃。

(If it's relevant, I can say that the crash has to do with manipulating a malloc-allocated array in the C++ DLL. I tracked it down by painstakingly commenting out blocks of code, rebuilding, running from Windows, & checking whether or not the crash occurs. However, I've reached a point where it's become very difficult to proceed without being able to properly break in the debugger). (如果它是相关的,我可以说崩溃与操纵C ++ DLL中的malloc分配的数组有关。我通过精心评论代码块,重建,从Windows运行以及检查是否跟踪它来查找它。发生了崩溃。然而,我已经达到了一个难以在没有能够在调试器中正确中断的情况下继续进行的程度)。

I'm just interested in being able to recreate the crash within Visual Studio. 我只对能够在Visual Studio中重新创建崩溃感兴趣。

When a program works in debug but crash in release, the problem is really often buffer overflow, so you should look for something like an incorrect buffer length variable. 当程序在调试中工作但在发布时崩溃时,问题通常是缓冲区溢出,所以你应该寻找类似不正确的缓冲区长度变量。

About why it is not crashing when debugging, here is a small article about the side effects of debugger . 关于调试时它没有崩溃的原因,这里有一篇关于调试器副作用的小文章。 As you can read, the heap can have a different behavior when a program is launched by a debugger. 正如您所看到的,当调试器启动程序时,堆可能会有不同的行为。 Buffer overflows often happen on the heap, and you said it might happen with a malloc-ed buffer, so that would be the reason. 缓冲区溢出经常发生在堆上,你说它可能发生在malloc-ed缓冲区中,所以这就是原因。

Now, to make your program crash while debugging, the only way is probably to attach after launch. 现在,为了使程序在调试时崩溃,唯一的方法可能是在启动后附加。 If setting breakpoints in the DLL project doesn't work, try to start the debugging with the DLL project instead, and specify your executable as DLL host. 如果在DLL项目中设置断点不起作用,请尝试使用DLL项目启动调试,并将可执行文件指定为DLL主机。 If you can't hit breakpoints like that, well, you can always set breakpoints in assembly code, that should always work, but isn't really practical. 如果你不能点击这样的断点,那么你总是可以在汇编代码中设置断点,这应该始终有效,但实际上并不实用。

Note that differences might still happen even when attaching, a debugger will always slightly alter the target behavior. 请注意,即使在附加时仍可能发生差异,调试器总是会略微改变目标行为。 Though only undefined behaviors should change. 虽然只有未定义的行为应该改变。

Edit: I missed that, but the article says that you can disable debug heap by setting an environment variable _NO_DEBUG_HEAP = 1 . 编辑:我错过了,但文章说您可以通过设置环境变量_NO_DEBUG_HEAP = 1来禁用调试堆。 That might be the simplest solution to debug your problem (if it works). 这可能是调试问题的最简单的解决方案(如果有效)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 简单的 C# 应用程序无法正常工作,在 Visual Studio 中运行时工作正常 - Simple C# application not working correctly, works fine when run from within Visual Studio 在 Visual Studio 中将数据从 C# 传递到 C++ - Pass Data from C# to C++ in Visual Studio 如何将控制台应用程序中的变量值传递给 C# 中的 windows 表单应用程序(Visual Studio) - How do i pass a variable value from Console application to windows form application in C# (visual studio) 如何从Windows C#(Visual Studio)运行cygwin编译的程序? - How do you run a cygwin-compiled program from windows c# (visual studio)? 从Visual Studio 2008中的C#应用​​程序调用时如何调试C DLL - How to debug C DLL when called from a C# application in Visual Studio 2008 在C#中运行时设置类属性会使Visual Studio崩溃 - Setting a class property at run time in c# crashes Visual Studio Visual C ++ / C#中的WinForms编程与Visual C ++(MFC)中的Windows编程有何不同 - How is WinForms programming in Visual C++/C# different from Windows programming in Visual C++(MFC) 如何从Visual Studio 2012导出C#应用程序以在其他计算机上运行? - how can I export a C# application from Visual Studio 2012 to run in other computers? 如何从C#应用程序运行C ++二进制代码 - How to run c++ binary code from c# application Visual Studio C#从Windows窗体打开控制台 - Visual Studio c# open console from windows form
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM