我在 Windows 上,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 创建)并使用它们替换主要产品中的那些可以解决问题。测试代码同上。发布代码使用 2005 年编译的 Qt,使用 msvcr80。