Not sure what's going on here.
I have an Windows console app written in C. When I run it from within VS2008, it runs fine. If I run it from the cmd.exe prompt, it crashes, usually in malloc(). I am guessing it is a race condition due to a mismatched CRT library.
不知道这里发生了什么。我有一个用C语言编写的Windows控制台应用程序。当我从VS2008中运行它时,它运行正常。如果我从cmd.exe提示符运行它,它会崩溃,通常在malloc()中。由于CRT库不匹配,我猜这是一种竞争条件。
The app is simple.
It calls into the WinHttp layer to send a GET request to a website, then slurps up the reply. That part seems to work fine, but after WinHttpReadData, the program calls printf() to print out the data received, and that is where the malloc crash often occurs.
该应用程序很简单。它调用WinHttp层向网站发送GET请求,然后悄悄回复。这部分似乎工作正常,但在WinHttpReadData之后,程序调用printf()来打印出收到的数据,这就是malloc崩溃经常发生的地方。
But only outside the debugger. ????
但只能在调试器之外。 ????
I am compiling from the command line.
我正在从命令行编译。
c:\vc9\bin\cl.exe /Zi /DEBUG -Ic:\vc9\Include
-IC:\WindowsSDK\v6.1\Include HttpGet.c
-link /debug /out:HttpGet.exe /SUBSYSTEM:CONSOLE /LIBPATH:c:\vc9\Lib
/LIBPATH:C:\WindowsSDK\v6.1\Lib WinHttp.lib
I see the results above if I compile with /MT, or nothing. If I compile with /MD, then it hangs when run in the debugger, on a call to free(), and it crashes in cmd.exe (same as with /MT).
如果我用/ MT编译,或者什么都没有,我看到上面的结果。如果我用/ MD编译,那么当它在调试器中运行时,在调用free()时会挂起,并且它在cmd.exe中崩溃(与/ MT相同)。
run in result: /MT result: /MD
--------- ------------ -----------
VS2008 debugger runs fine hang in free() (at the end)
cmd.exe crash in malloc crash in malloc
"VC cmd prompt" crash or hang(spin) ??
Some questions -
一些问题 -
-
Is the different behavior because of the PATH available within VS2008?
由于VS2008中的PATH可用,是不同的行为?
-
Could the cause be that I don't have the VC90 runtime installed on my machine?
原因可能是我的机器上没有安装VC90运行时?
-
I thought that by linking statically (/MT) I wouldn't have the requirement of needing the VC90 runtime to be installed?
我认为通过静态链接(/ MT)我不需要安装VC90运行时?
-
I still don't understand /NODEFAULTLIB. Is that relevant?
我还是不明白/ NODEFAULTLIB。这有关系吗?
I am used to makefiles and compilers, and I know C. I don't know C++ which is why I write in C. But I don't understand all the vagaries of the CRT on Windows. Can someone clear up this mystery?
我习惯于编写文件和编译器,我知道C.我不知道C ++这就是我用C编写的原因。但是我不了解Windows上CRT的所有变幻莫测。有人可以澄清这个谜吗?
1 个解决方案
#1
Typically when I've seen something work in the debugger but nowhere else, it's due to uninitialized memory. The debugger is "nice enough" to clear memory for you, as if that is doing you a favor.
通常情况下,当我在调试器中看到某些工作但其他地方没有工作时,这是由于未初始化的内存。调试器“足够好”,可以为您清除内存,就好像这对您有利。
The second possibility is a buffer overrun, and the debugger is causing the memory location of your mallocs to move around enough to avoid it. I would suspect this one given your failure is showing up during a malloc; you might be corrupting the malloc chain.
第二种可能是缓冲区溢出,调试器导致mallocs的内存位置移动到足以避免它。鉴于你的失败在malloc期间出现,我会怀疑这一个;你可能会破坏malloc链。
Another possibility that stands out is some sort of race condition, and the debugger is changing the timing enough to let you get away with it.
另一种突出的可能性是某种竞争条件,调试器正在改变时间,让你逃脱它。
#1
Typically when I've seen something work in the debugger but nowhere else, it's due to uninitialized memory. The debugger is "nice enough" to clear memory for you, as if that is doing you a favor.
通常情况下,当我在调试器中看到某些工作但其他地方没有工作时,这是由于未初始化的内存。调试器“足够好”,可以为您清除内存,就好像这对您有利。
The second possibility is a buffer overrun, and the debugger is causing the memory location of your mallocs to move around enough to avoid it. I would suspect this one given your failure is showing up during a malloc; you might be corrupting the malloc chain.
第二种可能是缓冲区溢出,调试器导致mallocs的内存位置移动到足以避免它。鉴于你的失败在malloc期间出现,我会怀疑这一个;你可能会破坏malloc链。
Another possibility that stands out is some sort of race condition, and the debugger is changing the timing enough to let you get away with it.
另一种突出的可能性是某种竞争条件,调试器正在改变时间,让你逃脱它。