为什么我在打开的FILE*上使用QTextStream时出错
Why am I getting a crash using QTextStream on an open FILE *
我在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版本的危险
- 访问者访问变体并返回不同类型时出错
- 在Linux for Windows上编译C++代码时出错
- 读取文件的最后一行并输入到链接列表时出错
- 重载操作程序时出错>>用于类中的字符串 memebr
- 调用专用模板时出错"no matching function for call to [...]"
- C++-试图将函数指针推回到另一个CPP文件中的矢量时出错
- LINK 尝试使用 OpenSSL evp aes 256 c++ 时出错
- 在Google Kick start中提交时出错
- 在c++中访问int到类对象的映射时出错
- 分段错误当我试图运行程序时出错
- 使用dynamic_cast和构造函数时出错
- CHECK(调用)函数在Google Colab中出错
- 用pybind11包装C++抽象类时出错
- 为x86而非x64编译时出错
- 从R调用C++函数并对其进行集成时出错
- 这个函数哪里出错了
- C++ 创建包含链表和字符串的对象的链接列表时出错
- 为重写std::exception的库生成swig接口时出错
- 导入/导出变量时出错
- 为什么我在打开的FILE*上使用QTextStream时出错