我在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"只要。然后下面的注释链接到here,表明应该以二进制模式打开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。
答案 0 :(得分:1)
@KarstenKoop的所有功劳 - 随时在此处发布您的答案。问题是由于使用msvcr80.dll的Qt dll,而应用程序的其余部分是使用visual studio 2010编译的,因此使用了msvcr100.dll
这link解释了混合视觉工作室版本的危险