R
R
Robotex2011-03-10 09:52:38
Qt
Robotex, 2011-03-10 09:52:38

Qt4: application crashes when trying to read quint32 variable from QDataStream?

For some reason, the application crashes when reading several variables from the stream (here they are in the private section):

#ifndef CWAVE_H
#define CWAVE_H

#include <QBuffer>
#include <QByteArray>
#include <QDataStream>
#include <QDebug>

class CWave
{
public:
    CWave();

    qint8 loadWave(QByteArray fileData);

private:
    quint16 compressionCode;
    quint16 numberOfChannels;
    quint32 sampleRate;
    quint32 averageBytesPerSecond;
    quint16 blockAlign;
    quint16 significantBitsPerSample;

};

#endif // CWAVE_H

If I comment out the reading of the sampleRate and averageBytesPerSecond variables, then the program works fine, but it crashes with them (see the output below)
#include "cwave.h"

CWave::CWave()
{
}

qint8 CWave::loadWave(QByteArray fileData)
{
    QBuffer buffer(&fileData);

    buffer.open(QIODevice::ReadOnly);

    QDataStream stream(&buffer);
    stream.setByteOrder(QDataStream::LittleEndian);

    quint32 chunkID;
    quint32 chunkDataSize;
    quint32 riffType;

    stream.setByteOrder(QDataStream::BigEndian);
    stream >> chunkID;

    stream.setByteOrder(QDataStream::LittleEndian);
    stream >> chunkDataSize;

    stream.setByteOrder(QDataStream::BigEndian);
    stream >> riffType;


    qDebug() << chunkID << chunkDataSize << riffType;

    if(chunkID != 0x52494646 || riffType != 0x57415645) // not RIFF or WAVE
        return -1;

    if(chunkDataSize != buffer.size()-8)
        return -2;

    while(!stream.atEnd())
    {
        stream.setByteOrder(QDataStream::BigEndian);
        stream >> chunkID;

        stream.setByteOrder(QDataStream::LittleEndian);
        stream >> chunkDataSize;

        qDebug() << chunkID << chunkDataSize;

        switch(chunkID)
        {
        case 0x666D7420:    //fmt
            // вот тут вот проблема какая-то
            stream >> compressionCode;
            stream >> numberOfChannels;
            stream >> sampleRate;
            stream >> averageBytesPerSecond;
            stream >> blockAlign;
            stream >> significantBitsPerSample;


            qDebug() << compressionCode << numberOfChannels << sampleRate << averageBytesPerSecond << blockAlign << significantBitsPerSample;

            if(chunkDataSize > 16)
                //buffer.seek(buffer.pos() + chunkDataSize - 16);
                qDebug() << stream.skipRawData(chunkDataSize - 16);

            break;
        case 0x64617461:      //data

            //break;
        default:
            //buffer.seek(buffer.pos() + chunkDataSize);
            qDebug() << stream.skipRawData(chunkDataSize);
        }
    }

    buffer.close();

    stream.unsetDevice();

    return 0;
}

Usage:
void MainWindow::loadFile(QString fileName)
{
    QFile file(fileName);
    if (!file.open(QFile::ReadOnly)) {
        QMessageBox::warning(this, tr("Error!"),
                             tr("Cannot read file %1:\n%2.")
                             .arg(fileName)
                             .arg(file.errorString()));
        return;
    }

    QByteArray fileData = file.readAll();

    file.close();

    qint8 returnValue = wave.loadWave(fileData);

    switch(returnValue)
    {
    case -1:
        QMessageBox::warning(this, tr("Not a wave!"),
                             tr("Wrong file type."));
        break;
    case -2:
        QMessageBox::warning(this, tr("Not a wave!"),
                             tr("Wrong file size."));
        break;

    case 0:
        qDebug() << "success!";
        break;
    }
}

Conclusion:
Starting /home/robotex/workspace/speech-build-desktop/speech...<br/>
1380533830 221018 1463899717 <br/>
1718449184 16 <br/>
1 1 8000 16000 2 16 <br/>
1684108385 220982 <br/>
220982 <br/>
success! <br/>
The program has unexpectedly finished.

Moreover, when I made a compressionCode variable of type quint32, the program crashed with an error:
pure virtual method called<br/>
terminate called without an active exception<br/>
The program has unexpectedly finished.

What could be the problem?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
K
korvindest, 2011-11-18
@korvindest

With the modal window, everything is clear, as long as it hangs for you at least a little, the object has time to synchronize and is already sent to C # in its entirety.
Solving this with a delay is not an option, but for the test you can try to put just a big wait (or Delay, I don’t remember what it’s called in Delphi).
You must make sure that when you pass control to C#, the object in memory already contains what you are passing.
This is all speculation, of course, but I think that's the way it is.

G
GavriKos, 2011-11-18
@GavriKos

You can try this mechanic - when you call a library function, you betray a callback into it. When the library accurately renders the picture (I hope this can be verified), it will call the main application's callback.

S
shsweb, 2011-11-18
@shsweb

Try instead of calling a modal window:

for i:=0 to 100 do Application.ProcessMessages;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question