仅当从发布可执行文件运行时,fopen 才会崩溃
我多次调用从输入文件读取数据的函数。在调试模式下一切工作正常,但是当我尝试从发布模式运行可执行文件时,带有 fopen 的行在几次调用后使程序崩溃。我的代码是: 来自头文件:
#define presstankdatabase "presst_database.txt"
在函数中:
FILE *fidread;
fidread = fopen(presstankdatabase,"r");
if (fidread==NULL) {
printf("Failed to open pressurant tank database: %s\n",presstankdatabase);
return 1;
}
在调试中,我在以 fidread = 开头的行之前和之后插入了注释行,经过几次调用后,程序崩溃,并收到消息“出现问题导致程序停止正常工作”请关闭该程序。”显示 fopen 调用之前的注释,但不显示之后的注释。我对 fopen 的理解是它应该返回一个指针或 NULL,但它在到达检查之前就崩溃了。我唯一能想到的是,不知怎的,我遇到了内存问题,但我不知道这如何适应 fopen 崩溃。有谁知道会发生什么?谢谢!
编辑1:我增加了三个变量的大小,它们唯一使用的地方(除了 printf() 调用)如下所示。
char *constid = (char*)malloc(sizeof(char)*20);
像这样使用:
strcpy(constid,"Propellant");
strcpy(constid,"Propellant tank");
strcpy(constid,"Pressurant tank");
如果变量的大小为 20,如上所示,它会崩溃。但如果它们更大(我尝试过 120 和 100),程序就会运行。除了 fprintf() 或 printf() 调用之外,这些变量不会在任何其他地方使用。
I make several calls to a function that reads data from an input file. Everything works fine in debug mode, but when I try to run the executable from release mode, the line with fopen crashes the program after a few calls. My code is:
From header file:
#define presstankdatabase "presst_database.txt"
In function:
FILE *fidread;
fidread = fopen(presstankdatabase,"r");
if (fidread==NULL) {
printf("Failed to open pressurant tank database: %s\n",presstankdatabase);
return 1;
}
In debugging, I've inserted comment lines just before and just after the line starting with fidread =, and after several calls the program crashes and I get the message "A problem caused the program to stop working correctly. Please close the program." The comment just before the fopen call is displayed, but the comment just after does not. My understanding of fopen is that is should return either a pointer or NULL, but it crashes before it even gets to the check. The only thing I can think of is that somehow I'm having memory problems, but I don't know how that would fit in with fopen crashing. Does anyone know what might be going on? Thanks!
EDIT 1: I increased the size of three variables, and the only places they're used (except in printf() calls), are as shown below.
char *constid = (char*)malloc(sizeof(char)*20);
Used like so:
strcpy(constid,"Propellant");
strcpy(constid,"Propellant tank");
strcpy(constid,"Pressurant tank");
If the variables are sized to 20, as shown above, it crashes. But if they're larger (I've tried 120 and 100), the program runs. The variables aren't used in any other places other than fprintf() or printf() calls.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
presstankdatabase
应该是一个指向包含要打开的文件名的字符串的指针。如果fopen()
崩溃,则该指针可能无效(或 NULL)。如果没有更多代码,就不可能进一步调试它。使用 VC 调试器查看发生了什么...编辑:
另一个常见原因是文件名字符串突然停止以 NULL 结尾。
您应该添加一个
printf()
调用来在打开之前打印文件名。它很可能无法产生预期的输出。如果没有,那么你就会遇到一种更有趣的内存损坏形式,需要更多的工作才能清除。编辑 2:
如果
printf()
调用显示正确的字符串,那么您的代码中其他地方可能存在内存损坏,破坏了 C 库的某些内部结构。一个常见的原因是超出了静态数组的末尾(或开头)或malloc()
提供的区域。presstankdatabase
should be a pointer to a string containing the filename to open. Iffopen()
crashes then that pointer is probably invalid (or NULL). Without any more code it is not possible to debug it further. Use the VC debugger to see what's happening...EDIT:
Another common cause of this is a filename string that suddenly stops being NULL-terminated.
You should add a
printf()
call to print the filename before opening. It will most probably fail to produce the expected output. If not, then you have a more interesting form of memory corruption that will take some more work to weed out.EDIT 2:
If the
printf()
call shows the correct string, then you probably have memory corruption somewhere else in your code that has mangled some internal structure of the C library. A common cause is going beyond the end (or the beginning for that matter) of a static array or a region provided bymalloc()
.