c++ - OpenAL Soft 在 Release模式下崩溃(调试工作正常)

标签 c++ openal-soft

我用 C++ 创建了一个小示例程序,试图用 OpenAL Soft 播放声音。程序在 Release模式下编译时崩溃,在 Debug模式下编译时它可以工作。我在 OS X 上使用 1.17.2。

我收到此错误消息:

SoundTest(28958,0x70000021d000) malloc: *** error for object 0x7fbdd26062c8: incorrect checksum for freed object - object was probably modified after being freed.

这是完整的示例程序:

#include <iostream>
#include <AL/alc.h>
#include <AL/al.h>
#include <cmath>

using namespace std;

int main() {
    // Reset error state just to be sure
    alGetError();

    ALCdevice *device = alcOpenDevice(NULL);
    if (device == NULL) {
        cout << "Error creating device" << endl;
        return 1;
    }

    ALCcontext *context = alcCreateContext(device, NULL);
    if (!alcMakeContextCurrent(context)) {
        cout << "Failed to make context current" << endl;
        return 1;
    }

    ALuint buffer;

    // Set up sound buffer
    alGenBuffers((ALuint)1, &buffer);

    // Fill buffer with sine-wave
    float freq = 440.f;
    int seconds = 4;
    unsigned sample_rate = 22050;
    size_t buf_size = seconds * sample_rate;

    short *samples;
    samples = new short[buf_size];
    for(int i=0; i<buf_size; ++i) {
        samples[i] = (short) (32760 * sin((2.f * float(M_PI) * freq) / sample_rate * i ));
    }

    alBufferData(buffer, AL_FORMAT_MONO16, samples, buf_size, sample_rate);

    ALuint source;

    // Set up sound source
    alGenSources((ALuint)1, &source);
    alSourcef(source, AL_PITCH, 1);
    alSourcef(source, AL_GAIN, 1);
    alSource3f(source, AL_POSITION, 0, 0, 0);
    alSource3f(source, AL_VELOCITY, 0, 0, 0);
    alSourcei(source, AL_LOOPING, AL_FALSE);
    alSourcei(source, AL_BUFFER, buffer);

    // Start playing
    alSourcePlay(source);

    // Wait until the sound stops playing
    ALenum state;
    do {
        alGetSourcei(source, AL_SOURCE_STATE, &state);
    } while (state == AL_PLAYING);

    // Clean up
    alDeleteSources(1, &source);
    alcMakeContextCurrent(NULL);
    alcDestroyContext(context);
    alcCloseDevice(device);

    return 0;
}

最佳答案

如果其他人遇到同样的问题,请提供 look at this .问题现已解决。

关于c++ - OpenAL Soft 在 Release模式下崩溃(调试工作正常),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37889119/

相关文章:

c++ - 在 Windows 7 上通过 QTcpSocket 流式传输图像

c++ - 您可以在不使用 Core Audio API 的情况下更改 Windows 中 volume mixer/sndvol 中列出的程序名称吗?

c++ - alcOpenDevice : AL_INVALID_OPERATION 中的 OpenAL 软 40964

android - 开软+hrtf

c++ - vector 重新分配后迭代器如何更新

c++ - 如何使用 C++ win32 API 为 Active Directory 中的单个属性设置和获取多个值(数组值)?

c++ - 创建 const std::vector 作为两个 const std::vector 的串联

c++ - 定义调用约定有什么意义?