# Rev. 8875 (developm.) = changed move constructor / move ass.op. for AudioBuffer.channels crashes my app

**URL:** <https://forum.juce.com/t/rev-8875-developm-changed-move-constructor-move-ass-op-for-audiobuffer-channels-crashes-my-app/24055>\
**Category:** Development\
**Created:** [September 5, 2017, 1:23pm UTC](https://forum.juce.com/t/rev-8875-developm-changed-move-constructor-move-ass-op-for-audiobuffer-channels-crashes-my-app/24055 "2017-09-05T13:23:40Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![instrumentMakers](https://avatars.discourse-cdn.com/v4/letter/i/bb73d2/32.png) [@instrumentMakers](https://forum.juce.com/u/instrumentMakers)\
**Post date:** [September 5, 2017, 1:23pm UTC](https://forum.juce.com/t/rev-8875-developm-changed-move-constructor-move-ass-op-for-audiobuffer-channels-crashes-my-app/24055/1 "2017-09-05T13:23:40Z")

</div>

@hogilux: You changed the move constructor / move assignment for AudioBuffer.channels. I don’t know why, but since then I get random crashes in several parts of my app at startup. Could you have look into this again?

---

<div class="post-metadata">

**Author:** ![daniel](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/daniel/32/790_2.png) [@daniel](https://forum.juce.com/u/daniel)\
**Post date:** [September 5, 2017, 1:28pm UTC](https://forum.juce.com/t/rev-8875-developm-changed-move-constructor-move-ass-op-for-audiobuffer-channels-crashes-my-app/24055/2 "2017-09-05T13:28:21Z")

</div>

For the records: hogilux is @Fabian 😉

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [September 5, 2017, 1:38pm UTC](https://forum.juce.com/t/rev-8875-developm-changed-move-constructor-move-ass-op-for-audiobuffer-channels-crashes-my-app/24055/3 "2017-09-05T13:38:16Z")

</div>

Those methods look OK to me… Which revision are you talking about? (“8875” isn’t a git ID, is it?)

---

<div class="post-metadata">

**Author:** ![instrumentMakers](https://avatars.discourse-cdn.com/v4/letter/i/bb73d2/32.png) [@instrumentMakers](https://forum.juce.com/u/instrumentMakers)\
**Post date:** [September 5, 2017, 1:41pm UTC](https://forum.juce.com/t/rev-8875-developm-changed-move-constructor-move-ass-op-for-audiobuffer-channels-crashes-my-app/24055/4 "2017-09-05T13:41:12Z")

</div>

Sorry, we’re using old fashioned svn. I’ll try to figure out the Git ID…

---

<div class="post-metadata">

**Author:** ![instrumentMakers](https://avatars.discourse-cdn.com/v4/letter/i/bb73d2/32.png) [@instrumentMakers](https://forum.juce.com/u/instrumentMakers)\
**Post date:** [September 5, 2017, 1:46pm UTC](https://forum.juce.com/t/rev-8875-developm-changed-move-constructor-move-ass-op-for-audiobuffer-channels-crashes-my-app/24055/5 "2017-09-05T13:46:21Z")

</div>

No Git ID, but date/time: 2017/08/29 12:39:26

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [September 5, 2017, 1:53pm UTC](https://forum.juce.com/t/rev-8875-developm-changed-move-constructor-move-ass-op-for-audiobuffer-channels-crashes-my-app/24055/6 "2017-09-05T13:53:52Z")

</div>

Hmm. Fabian’s fix looks correct to me, and the previous version definitely had a bug in it. I can’t really see how the old one would work for you and the new one fail, unless you’ve got some kind of corrupted memory/dangling pointer issue and just happened to be lucky before!

---

<div class="post-metadata">

**Author:** ![instrumentMakers](https://avatars.discourse-cdn.com/v4/letter/i/bb73d2/32.png) [@instrumentMakers](https://forum.juce.com/u/instrumentMakers)\
**Post date:** [September 5, 2017, 3:10pm UTC](https://forum.juce.com/t/rev-8875-developm-changed-move-constructor-move-ass-op-for-audiobuffer-channels-crashes-my-app/24055/7 "2017-09-05T15:10:55Z")

</div>

In some cases, I allocate an AudioBuffer on the Stack and copy it to a class member just like this:

```ruby
someClassMemberAudioSampleBuffer = AudioSampleBuffer (2, samplesPerBlock);

```

This doesn’t work any more.

I think I now understand why:

In

```ruby
AudioBuffer& operator= (AudioBuffer&& other) noexcept

```

this line

```ruby
channels = numChannels < (int) numElementsInArray (preallocatedChannelSpace) ? preallocatedChannelSpace : other.channels;

```

is wrong. “other.channels” is never used, because (int) numElementsInArray (preallocatedChannelSpace) is 32. And preallocatedChannelSpace is an Array filled with nullptrs.

If I change it to

```ruby
channels = numChannels < (int) numElementsInArray (preallocatedChannelSpace) ? other.channels : preallocatedChannelSpace;

```

it works again…  
Could you verify this?

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [September 5, 2017, 3:28pm UTC](https://forum.juce.com/t/rev-8875-developm-changed-move-constructor-move-ass-op-for-audiobuffer-channels-crashes-my-app/24055/8 "2017-09-05T15:28:28Z")

</div>

OK… Your suggestion’s definitely not correct, you’re just re-introducing the bug that Fabian was trying to fix.

However, I can see an edge-case where if the source block had been shrunk it could copy the wrong thing into that preallocated data block… Does it fix it your problem if you just change this line:

```
        memcpy (preallocatedChannelSpace, other.channels, sizeof (preallocatedChannelSpace));

```

?

---

<div class="post-metadata">

**Author:** ![instrumentMakers](https://avatars.discourse-cdn.com/v4/letter/i/bb73d2/32.png) [@instrumentMakers](https://forum.juce.com/u/instrumentMakers)\
**Post date:** [September 5, 2017, 3:35pm UTC](https://forum.juce.com/t/rev-8875-developm-changed-move-constructor-move-ass-op-for-audiobuffer-channels-crashes-my-app/24055/9 "2017-09-05T15:35:48Z")

</div>

Sorry, I missed the memcpy line…

Yes, indeed, now it works like expected. Thank you.

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [September 5, 2017, 3:36pm UTC](https://forum.juce.com/t/rev-8875-developm-changed-move-constructor-move-ass-op-for-audiobuffer-channels-crashes-my-app/24055/10 "2017-09-05T15:36:29Z")

</div>

OK, thanks - I’ll push a fix to develop shortly!

---

<div class="post-metadata">

**Author:** ![instrumentMakers](https://avatars.discourse-cdn.com/v4/letter/i/bb73d2/32.png) [@instrumentMakers](https://forum.juce.com/u/instrumentMakers)\
**Post date:** [September 5, 2017, 3:38pm UTC](https://forum.juce.com/t/rev-8875-developm-changed-move-constructor-move-ass-op-for-audiobuffer-channels-crashes-my-app/24055/11 "2017-09-05T15:38:09Z")

</div>

Great.

---

<div class="post-metadata">

**Author:** ![jrlanglois](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/jrlanglois/32/21372_2.png) [@jrlanglois](https://forum.juce.com/u/jrlanglois)\
**Post date:** [September 5, 2017, 4:42pm UTC](https://forum.juce.com/t/rev-8875-developm-changed-move-constructor-move-ass-op-for-audiobuffer-channels-crashes-my-app/24055/12 "2017-09-05T16:42:06Z")

</div>

> [@jules](#):
>
> However, I can see an edge-case where if the source block had been shrunk it could copy the wrong thing into that preallocated data block… Does it fix it your problem if you just change this line:

Requesting a unit test be added for that case. Presumably that issue could have been caught during a unit test running its course?

Edit: Unless I skimmed over it accidentally looking for UnitTest subclasses, it appears there isn’t an `AudioBuffer<float>` or `AudioBuffer<double>` UnitTest…

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [September 6, 2017, 7:55am UTC](https://forum.juce.com/t/rev-8875-developm-changed-move-constructor-move-ass-op-for-audiobuffer-channels-crashes-my-app/24055/13 "2017-09-06T07:55:55Z")

</div>

Agreed.

(Though TBH we’d have been lucky if any unit-test had caught an edge case like this one)
