Home > Mobile >  QT data collected by audio playback QIODevice: : write a continuous output will be a memory leak pro
QT data collected by audio playback QIODevice: : write a continuous output will be a memory leak pro

Time:01-19

QAudioOutput * output;
OutputDevice QIODevice *;
The output=NULL;

QAudioFormat format;
Format. SetSampleRate (8000);
Format. SetChannelCount (2);
Format. SetSampleSize (16);
Audio/PCM format. SetCodec (" ");
Format. SetSampleType (QAudioFormat: : SignedInt);
Format. SetByteOrder (QAudioFormat: : LittleEndian);
If (the output==NULL)
{
The output=new QAudioOutput (format, this);
The output - & gt; SetVolume (0.05);
OutputDevice=output - & gt; Start ().//start playing
}

While (1)
{
QThread: : msleep (1);
QByteArray array;
Int len_fVar=sizeof (fVar);
Array. The resize (len_fVar);
Memcpy (array data (), & amp; FVar len_fVar);
OutputDevice - & gt; Write (array, the array. The size ());
}
}
Is finally write out the problem and try to send after waitForBytesWritten and judgment before sending bytesToWrite () if can't solve the problem, ask for help!

CodePudding user response:

What is the fVar?

CodePudding user response:

FVar is collected data storage arrays

CodePudding user response:

What is the memory leak problem? To have too little information from you look not to come out in this code
Another has nothing to do with the memory leak problem, your side a bit redundant, there is no need to create a QByteArray, direct outputDevice - & gt; Write ((const char *) (& amp; FVar), sizeof (fVar)) don't have to do is
And you don't have to judge the result of the write how much (that is, the actual write), how do you for subsequent processing?

CodePudding user response:

Is always running memory will always rise, is really no need to turn the QBytearray, subsequent no processing is always write out the array data, fVar the length of the array is not more than 1 k bytes, even after judging the write () function returns the length of how to deal with? ! Below this post some, but I didn't quite understand:
https://bbs.csdn.net/topics/391027979
"QIODevice (QTcpSocket is a subclass of QIODevice) have the internal buffer, if not set fixed size, can according to the data you want to write, constantly to allocate memory, you can set a threshold, such as 512 k, before each write calls the bytesToWrite (), look at the rest of the not write data of how many, if close to you to set the threshold, just wait for the next round of call write, so that you can avoid memory fragments,"

CodePudding user response:

I looked at you again this code is problematic, you sleep, so have been circulating in write ah, have no opportunity to read the somebody else, so will always rise
Are you the array data is fixed, you have been writing a fixed data out like this?
You should have a look at the official Example Audio Output Example is how to write in it
  •  Tags:  
  • Qt
  • Related