繁体   English   中英

为什么我在打开的文件上使用QTextStream发生崩溃*

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

我在窗户上,qt 4.7。 我的部分代码从一个文件中获取一个FILE *,该文件可以被系统的另一部分(遗留,C)打开。 我按如下方式打开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建议,在FILE *上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;
}

发布模式中的以下所有内容:

如果我在visual studio外面运行,这可以让我附加一个调试器。 如果我在外部视觉工作室跑步,它会崩溃。 如果我从外部视觉工作室开始附加它,它会在QTextStream的构造上崩溃。 如果我使用shift-F5从visual studio内部启动它(即在调试器外运行),它会将文件内容写入显示器。 同样,当在调试器下运行时,它按预期工作。

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

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

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

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM