# FMOD reads data out of bounds

**URL:** <https://qa.fmod.com/t/fmod-reads-data-out-of-bounds/14155>\
**Category:** FMOD Engine\
**Created:** [October 23, 2018, 10:42am UTC](https://qa.fmod.com/t/fmod-reads-data-out-of-bounds/14155 "2018-10-23T10:42:34Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![guinzoo](https://avatars.discourse-cdn.com/v4/letter/g/e36b37/32.png) [@guinzoo](https://qa.fmod.com/u/guinzoo)\
**Post date:** [October 23, 2018, 10:42am UTC](https://qa.fmod.com/t/fmod-reads-data-out-of-bounds/14155/1 "2018-10-23T10:42:34Z")

</div>

My code is like this

```
FMOD::System_Create(&s_system);
s_system->setSoftwareFormat(44100, FMOD_SPEAKERMODE_STEREO, 0);
s_system->init(16, FMOD_INIT_NORMAL, nullptr);
...
void AudioManager::loadSound(Sound* sound, Sound::Info* info, U8* data) {
    FMOD_CREATESOUNDEXINFO exinfo;
    std::memset(&exinfo, 0, sizeof(exinfo));

    exinfo.cbsize = sizeof(exinfo);
    exinfo.length = info->dataSize;
    exinfo.numchannels = info->numChannels;
    exinfo.defaultfrequency = info->sampleRate;

    if (info->format == Format::PCM16) {
        exinfo.format = FMOD_SOUND_FORMAT_PCM16;
        exinfo.suggestedsoundtype = FMOD_SOUND_TYPE_RAW;

        s_system->createSound((char*)data, FMOD_LOWMEM | FMOD_CREATESAMPLE | FMOD_OPENMEMORY_POINT | FMOD_OPENRAW, &exinfo, (FMOD::Sound**)&sound->m_sound);
    }
}

void Sound::load(U8* data, U32 dataSize) {
    Info info;
    info.format = Format::PCM16;
    info.dataSize = dataSize;
    info.numChannels = 1;
    info.sampleRate = 22050;

    int inc = (info.format == Format::PCM16) * info.numChannels * 2;
    m_data = resourceManager().alloc(dataSize + inc); // U8* m_data;
    std::memcpy(m_data, data, dataSize);
    if (inc)
        std::memset(m_data + dataSize, 0x7f, inc);
     // std::memset(m_data + dataSize, 0, inc); // fix

    audioManager().loadSound(this, &info, m_data);
}

```

I’m using the latest Low level API 1.0.9 for MacOS.  
When I play short PCM16 sound with 22050 sample rate, I hear click at the end. To fix this I add zero bytes to the end of data. Is it normal behaviour?

EDIT:  
I add zeroes not to the end, but after it to fix clicking. Please see the code above.

---

<div class="post-metadata">

**Author:** ![cameron-fmod](https://yyz2.discourse-cdn.com/flex036/user_avatar/qa.fmod.com/cameron-fmod/32/261_2.png) [@cameron-fmod](https://qa.fmod.com/u/cameron-fmod)\
**Post date:** [October 25, 2018, 3:42am UTC](https://qa.fmod.com/t/fmod-reads-data-out-of-bounds/14155/2 "2018-10-25T03:42:54Z")

</div>

If your sample data doesn’t end at a zero crossing (or close to it) and there is no fade out then yes you will get a clicking sound when the sound cuts out.

---

<div class="post-metadata">

**Author:** ![guinzoo](https://avatars.discourse-cdn.com/v4/letter/g/e36b37/32.png) [@guinzoo](https://qa.fmod.com/u/guinzoo)\
**Post date:** [October 25, 2018, 8:45am UTC](https://qa.fmod.com/t/fmod-reads-data-out-of-bounds/14155/3 "2018-10-25T08:45:35Z")

</div>

> [@](#):
>
> If your sample data doesn’t end at a zero crossing (or close to it) and there is no fade out then yes you will get a clicking sound when the sound cuts out.

But my sound ends at zero. And I add zeroes not to sample, but after it. If I say FMOD not to use my buffer with FMOD\_OPENMEMORY\_POINT, there will be no clicking at all.
