在 VS 2008 中 CRT 调用 _osfile() 时出现断言错误?
我有一个已经运行了很长时间的 C++ 代码库。代码库是一组遗留的 VS 2003 项目,我最近将其迁移到 VS 2008。迁移似乎很成功,因为生成的程序已构建并运行。
我在新驱动器上重新安装了操作系统和所有应用程序,现在当我尝试在调试器中调试程序时,我在 CRT 的 chsize
内收到断言错误(实际上,_chsize_s)。具体来说(仅限要点,忽略安全检查):
FILE * testfile = fopen("P:\\_Dan\\local\\foogoo.txt", "w");
int filehandle = fileno(testfile);
chsize(filehandle, 0);
fwrite("goohoo", 1, 6, testfile);
fclose(testfile);
调试断言发生在 chsize
内 - 具体来说,在 CRT 的源代码文件 chsize.c 内,位于以下行:
_VALIDATE_CLEAR_OSSERR_RETURN_ERRCODE((_osfile(filedes) & FOPEN), EBADF);
... 其中 filedes
匹配文件句柄
。
我认为问题可能是由于新系统上没有安装旧版本的 VS(仅 VS 2008)造成的,因为某些第 3 方库需要 VS 8.0 可再发行版 - 即使在旧系统上似乎正在构建和运行使用 VS 2008 就可以了。因此我安装了 VS 2005(不是 2003)。然而,问题仍然存在。
任何建议都将受到极大的欢迎。
* 更新 - 该问题与 chsize
无关。请参阅下面我的回答。
I have a C++ code base that has been working for a long time. The code base was a legacy VS 2003 set of projects that I recently migrated to VS 2008. The migration seemed to be successful in that the resulting program built, and run.
I reinstalled my OS and all applications on a fresh drive, and now when I attempt to debug the program within the debugger, I receive an assertion error inside the CRT's chsize
(really, _chsize_s
). Specifically (cropped to essentials, ignoring safety checks):
FILE * testfile = fopen("P:\\_Dan\\local\\foogoo.txt", "w");
int filehandle = fileno(testfile);
chsize(filehandle, 0);
fwrite("goohoo", 1, 6, testfile);
fclose(testfile);
The debug assertion occurs within chsize
- specifically, within the CRT's source code file chsize.c, at the following line:
_VALIDATE_CLEAR_OSSERR_RETURN_ERRCODE((_osfile(filedes) & FOPEN), EBADF);
... where filedes
matches filehandle
.
I thought possibly the problem might result from not having an older version of VS installed on the new system (only VS 2008), because some 3rd-party libraries require VS 8.0 redistributable - even though on the old system things seemed to be building and running just fine using VS 2008. I therefore installed VS 2005 (not 2003). However, the problem continues to occur.
Any suggestions would be immensely welcome.
* Update - The issue is unrelated to chsize
. See my answer below.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
该问题已解决 - 与
chsize
无关。对于主项目,选择用于代码生成的 c 运行时库的链接模型设置为多线程调试 (/MTd),但对于它链接的解决方案中的所有项目,设置为多线程调试 DLL (/MDd)到。更改为 /MDd 解决了该问题。我熟悉这些链接问题,并且通常会小心地正确设置它们,但因为这是从 Visual Studio 早期版本的工作项目的升级,没有任何更改,所以我不认为要沿着这条路走下去。我没有调查如何或为何更改设置(或者即使在以前的版本中以这种方式设置但没有引起问题)。
The issue is resolved - and unrelated to
chsize
. The linking model to the c-runtime libraries chosen for code generation was set to multi-threaded debug (/MTd) for the main project, but multi-threaded debug DLL (/MDd) for all of the projects in the solution that it linked to. Changing to /MDd resolved the issue.I am familiar with these linking issues and am generally careful to set them properly, but because this was an upgrade of a working project from an earlier version of Visual Studio with no changes, I did not think to look down this road. I did not investigate how or why the setting was changed (or even if it was set this way in the previous version but did not cause problems).
我的代码中也发现了这个问题。
主程序需要与使用MT构建的共享库链接。
当主程序中打开的文件处理程序传递给共享库函数时,CRT 的 setmode.c 中的 _VALIDATE_RETURN((_osfile(fh) & FOPEN), EBADF, -1) 使程序崩溃。
在汇编模式下调试 _osfile,osfile 在表 __pioinfo (01802EEDB0h) 中查找文件处理程序。嗯,它是静态链接 CRT 中的固定区域。而主程序中的另一个__pioinfo是另一个地址01E619540h。
总之,如果两个模块需要共享全局数据,则不能使用MT模型来构建。
我只是想通过静态编译来优化共享库,可能会发生一些难以注意到的错误。
看来 GCC 的力量共享或静态在大多数情况下都是有意义的。
Found this problem in my code too.
Main program need to linked with shared library which build with MT.
When file handler opened in main program passed to shared library's function, _VALIDATE_RETURN((_osfile(fh) & FOPEN), EBADF, -1) in setmode.c of CRT crashed the program.
Debug the _osfile in assembly mode, osfile lookup file handler in table __pioinfo (01802EEDB0h). Well it's fixed area in statically linked CRT. And another __pioinfo in the main program is another address 01E619540h.
In one word, if two module need to shared global data, can not build with MT model.
I just want to optimize the shared library with static compilation, some hard to notice bugs could happen.
Seems GCC's force shared or static makes sense in most situation.