[Interest] Access Violation using QFile and QTextStream

Heidler, Kirstin (GE Oil & Gas) Kirstin.Heidler at ge.com
Fri Sep 14 13:44:00 CEST 2012


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;

}

 

Is there any way I can stop my memory from being freed? L

 

Thank you for your help,

Kirstin

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20120914/c96c8d30/attachment.html>


More information about the Interest mailing list