为什么我在打开的FILE*上使用QTextStream时出错

Why am I getting a crash using QTextStream on an open FILE *

本文关键字:QTextStream 出错 FILE 为什么      更新时间:2023-10-16

我在windows上,qt 4.7。我的部分代码从系统的另一部分(遗留,C)打开的文件中获取FILE*。我打开一个QTextStream如下:

// file currently opened (readonly), and partially read
FILE *infile = function_to_get_file_pointer(); 
QTextStream is(infile, QIODevice::ReadOnly);

第二行在发布模式下构建时崩溃,但在调试中正常。我可以逐步浏览调试版本,并查看QTextStream内部打开的QFile。在崩溃时,我在发布模式下从windows调用堆栈中获得的最多信息如下:

ntdll.dll!77450226()    
[Frames below may be incorrect and/or missing, no symbols loaded for ntdll.dll] 
ntdll.dll!77450142()    
msvcr80.dll!_lock_file(_iobuf * pf=0x71962148)  Line 241 + 0xa bytes    C
msvcr80.dll!_ftelli64(_iobuf * stream=0x71962148)  Line 51 + 0x8 bytes  C
QtCore4.dll!6708b87d()  
QtCore4.dll!67099294()  
QtCore4.dll!6713d491()  

这可能是转移注意力,但在试图锁定文件时似乎出了问题。在此之前,我已经为代码启用了调试输出,所以我知道是QTextStream的创建导致了问题。我欢迎任何建议!

经过进一步的挖掘,我发现这个文件虽然是ASCII,但最初是用"rb"打开的,目的是阻止win32 CRT将行结尾从\r\n转换为\n。我认为这会让Qt感到困惑,所以将fopen修改为仅使用"r"。然后下面的一条注释链接到这里,它显示FILE*应该以二进制模式打开,例如"rb",所以这不是问题。

尝试下面的tezrigs建议,文件上的freopen*给出以下内容:

msvcr100.dll!_crt_debugger_hook(int _Reserved=8633404)  Line 65 C
    msvcr100.dll!_call_reportfault(int nDbgHookCode=2, unsigned long dwExceptionCode=3221226519, unsigned long dwExceptionFlags=1)  Line 167 + 0x6 bytes    C++
    msvcr100.dll!_invoke_watson(const wchar_t * pszExpression=0x00000000, const wchar_t * pszFunction=0x00000000, const wchar_t * pszFile=0x00000000, unsigned int nLine=0, unsigned int pReserved=8633376)  Line 155 + 0xf bytes   C++
    msvcr100.dll!_invalid_parameter(const wchar_t * pszExpression=0x00000000, const wchar_t * pszFunction=0x00000000, const wchar_t * pszFile=0x00000000, unsigned int nLine=0, unsigned int pReserved=0)  Line 110 + 0x14 bytes    C++
    msvcr100.dll!_invalid_parameter_noinfo()  Line 121 + 0xc bytes  C++
    msvcr100.dll!_freopen_helper(_iobuf * * pfile=0x0083bc3c, const char * filename=0x00000000, const char * mode=0x013ee350, _iobuf * str=0x71962148, int shflag=64)  Line 31 + 0x1f bytes C
    msvcr100.dll!freopen(const char * filename=0x00000000, const char * mode=0x013ee350, _iobuf * str=0x71962148)  Line 111 C

传递给_call_report_fault的异常代码为0x0000417-致命错误:未知软件异常,这没有多大帮助。。

好的:更多的细节,以及一些自包含的、可复制的代码(myfile.txt必须超过1000个字符长):

#include <QtCore/QCoreApplication>
#include "qtextstream.h"
#include <iostream>
int main(int argc, char *argv[])
{ 
 // QCoreApplication a(argc, argv);  
  std::cin.get();
  FILE *myfile = fopen("myfile.txt", "rb");
  int c;
  for(int i=0; i < 1000; i++)
    c = getc(myfile);
  fflush(myfile);
  long pos = ftell(myfile);
  QTextStream is(myfile, QIODevice::ReadOnly);
  while(!is.atEnd())
  {
     QString in_line = is.readLine();
     std::cout << in_line.toStdString();
  }
  fseek(myfile, pos, SEEK_SET);
  fclose(myfile);
  return 0;
}

以下所有内容均处于发布模式:

如果我在visualstudio之外运行,这可以让我附加一个调试器。如果我跑到视觉工作室外面,它就会崩溃。一旦它从视觉工作室外开始,如果我把它连接起来,它就会在QTextStream的构建中崩溃。如果我用shift-F5从visualstudio内部启动它(即在调试器外部运行),它会将文件的内容写入显示器。同样,当在调试器下运行时,它也能按预期工作。

这取决于dll。我有一组本地编译的dll(使用MSVC2010创建),使用它们来替换主产品中的dll可以解决问题。测试代码同上。发布代码使用2005年编译的Qt,使用msvcr80。

所有功劳都归功于@KarstenKoop-请随时在此处发布您的答案。该问题是由于Qt dll使用msvcr80.dll,而应用程序的其余部分是使用visual studio 2010编译的,因此使用msvcr100.dll

这个链接很好地解释了混合visual studio版本的危险