[Interest] Access Violation using QFile and QTextStream

Heidler, Kirstin (GE Oil & Gas) Kirstin.Heidler at ge.com
Fri Sep 14 14:42:58 CEST 2012


Hi,

thank you, I also found a different solution. I am not sure why it is working, but it does.
I simply  create the file object as a pointer to the file object like that:
   QFile *file;
    file = new QFile(fileName);
    file->open(QIODevice::ReadOnly);

Thank you for the hint. :)
Kirstin

-----Ursprüngliche Nachricht-----
Von: interest-bounces+kirstin.heidler=ge.com at qt-project.org [mailto:interest-bounces+kirstin.heidler=ge.com at qt-project.org] Im Auftrag von Thomas McGuire
Gesendet: Freitag, 14. September 2012 13:56
An: interest at qt-project.org
Betreff: Re: [Interest] Access Violation using QFile and QTextStream

Hi,

In the FileParser constructor, you create a QFile object on the stack. This means that the QFile object will get destroyed when the constructor is done.
Then you create a QTextStream and pass that a pointer to the QFile. This pointer will however point to invalid memory after the constructor has finished, as the QFile will have been destroyed.
All further accesses to the QTextStream then internally try to use the invalid pointer to the already destroyed QFile, which will cause a access violation.
A solution here would be to make the QFile a member variable as well, so that it has a longer lifetime than just in the constructor.

Regards,
Thomas

On Friday 14 September 2012 13:44:00 Heidler, Kirstin (GE Oil & Gas) wrote:
> Here is what I do:
> 
> I open a File with QFile. Next I create a QTextStream for that file. 
> This TextStream is saved in a member to my FileParsing-class.
> 
> While still in the constructor everything works fine. I can access the 
> TextStream, print it's device's size etc.
> 
> The problem arises once my TestCase calls a function to read a line.
> 
> In that function of my File-Parsing class nothing seems to be working. 
> I want to use seek but that does not work. Printing the size also no 
> longer works.
> 
> When I run the application normally I don't get an error at all. The 
> only thing that happens is, that only one testcase is executed (the 
> default one, that should always pass).
> 
> The testcase runner however does not notify me, that there was an error.
> 
> When I debug the application I get a write access violation. (Code:
> 0xc0000005) at: 0x8, flags = 0x0;
> 
> This sounds to me like the memory was somehow freed??? I don't 
> understand why...
> 
> 
> 
> Here are some code examples:
> 
> The testfunction that does the calls:
> 
> void FileParserTest::testNameReadCorrectly(){
> 
>      FileParser parser = FileParser::FileParser(
> 
>                 "C:/Users/108015407/Documents/DatenSolidAxle/Seite
> A/GE_TDGS_3.5_S56°.GE.20°-80°.ERA.600.2012.2.zeu.Takt 1.txt");
> 
>     parser.readName();
> 
>     QString name = "GE_TDGS_3.5_S56°.GE.20°-80°.ERA.600.2012.2.zeu";
> 
>     QCOMPARE(parser.m_summary.getName(), name);
> 
> }
> 
> 
> 
> Relevant header of the parser:
> 
> #ifndef FILEPARSER_H
> #define FILEPARSER_H
> #include "scanSummary.h"
> #include <iostream>
> #include <string>
> #include <QFile>
> #include <QString>
> #include <QTextStream>
> class FileParser
> {
> public:
>     FileParser(char* fileName);
>     ~FileParser();
>     ScanSummary m_summary;
>     void readName();
> 
> protected:
>     QTextStream *m_file;
> };
> #endif // FILEPARSER_H
> 
> 
> 
> 
> The relevant functions:
> 
> FileParser::FileParser(char *fileName){
> 
>     QFile file;
> 
>     file.setFileName(fileName);
> 
>     if(!file.exists()){
> 
>         std::cout << "File not exist" << std::endl;
> 
>     }
> 
>     bool succ = file.open(QIODevice::ReadWrite);
> 
>     std::cout << succ << std::endl;
> 
>     if(!succ){
> 
>         std::cout << "Not Good!" << std::endl;
> 
>     }
> 
>     m_file = new QTextStream(&file);
> 
>     std::cout << m_file->device()->size() << std::endl;
> 
> }
> 
> FileParser::~FileParser(){
> 
>     m_file->device()->close();
> 
> }
> 
> 
> 
> void FileParser::readName(){
> 
>     if(!m_file->device()){
> 
>         std::cout << "Alarm..." << std::endl;
> 
>     }
> 
>     std::cout << "I got here: 4.05" << std::endl; //gets to this mark 
> but not the next
> 
>     //std::cout << m_file->device()->size() << std::endl; //these two 
> lines don't work either
> 
>     //std::cout << m_file->pos() << std::endl;
> 
>     bool succ = m_file->seek(0);
> 
>     std::cout << succ << std::endl;
> 
>     std::cout << "I got here: 4.1" << std::endl;
> 
>     QString line;
> 
>     std::cout << "I got here: 4.2" << std::endl;
> 
>     line = m_file->readLine();
> 
>     std::cout << "I got here: 4.3" << std::endl;
> 
>     m_summary.setName(line);
> 
>     std::cout << "I got here: 4.4" << std::endl;
> 
> }

--
** Qt Developer Conference: http://qtconference.kdab.com/ **

Thomas McGuire | thomas.mcguire at kdab.com | Software Engineer KDAB (Deutschland) GmbH&Co KG, a KDAB Group company Tel. Germany +49-30-521325470, Sweden (HQ) +46-563-540090 KDAB - Qt Experts - Platform-independent software solutions



More information about the Interest mailing list