c++ - QAudioOutput 代码是否存在内存泄漏?

标签 c++ qt qthread

我正在 QThread 中播放音频流,如下所示:

// Setup
QAudioFormat format;
format.setFrequency(44100);
format.setChannels(2);
format.setSampleSize(16);
format.setCodec("audio/pcm");
format.setByteOrder(QAudioFormat::LittleEndian);
format.setSampleType(QAudioFormat::SignedInt);
QAudioDeviceInfo info(QAudioDeviceInfo::defaultOutputDevice());
format = info.nearestFormat(format);
this->m_AudioOutput = new QAudioOutput(format, this);
DECLARE_ALIGNED(16,uint8_t,audio_buffer)[(AVCODEC_MAX_AUDIO_FRAME_SIZE * 3) / 2];

// Playback
QIODevice *iodevice = this->m_AudioOutput->start();
for(;;) {
    // Routine that fetches audio data from network
    // data_size is length of the buffer
    fetch_packet(&audio_buffer, data_size);

    qint64 dataRemaining = data_size;
    const char *b2 = (const char *)audio_buffer;
    while (dataRemaining) {
    qint64 bytesWritten = iodevice->write((const char *)b2, dataRemaining);
        dataRemaining -= bytesWritten;
        b2 = b2 + bytesWritten ;
    }

    msleep(10);
}

音频播放得很好,但应用程序的内存消耗似乎随着时间的推移而增加(大约每分钟 2MB)。我想知道我是否做错了什么。我想 QAudioOutput 应该负责在读取并用于播放后删除 QIODevice 的缓冲区?

最佳答案

我不这么认为,文档说:

开始播放音频流只需使用 QIODevice 调用 start() 即可。 QAudioOutput 然后将从 io 设备获取所需的数据。

它只是读取数据。 QIODevice 应该管理缓冲区。可以肯定的是,您可以使用 QIODevice::size() 检查缓冲区的大小并查看它是否在增长。

关于c++ - QAudioOutput 代码是否存在内存泄漏?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13995869/

相关文章:

c++ - Eclipse:GNU 工具链 - 使用 g++ 编译的 C 文件,重复 GNU C

c++ - 如何在 C++ 中删除换行符

使用迭代器时 C++ vector 内容未更改

c++ - 使用 QTableView 时将 QCombobox 选定文本设置为特定数据列

c++ - 在 Qt 中显示控制台

c++ - 通过 C++ 接口(interface)访问 PostgreSQL(链接器错误)

c++ - 如何将事件悬停在标签上

multithreading - QThread 中的 Worker

python - 槽在哪个线程中执行,我可以将其重定向到另一个线程吗?

C++/Qt - 从一个线程到另一个线程槽的信号