[英]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.