# Read access violation exception on AudioBuffer

**URL:** <https://forum.juce.com/t/read-access-violation-exception-on-audiobuffer/23035>\
**Category:** General JUCE discussion\
**Created:** [June 23, 2017, 8:40am UTC](https://forum.juce.com/t/read-access-violation-exception-on-audiobuffer/23035 "2017-06-23T08:40:11Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Cauchy](https://avatars.discourse-cdn.com/v4/letter/c/a88e57/32.png) [@Cauchy](https://forum.juce.com/u/Cauchy)\
**Post date:** [June 23, 2017, 8:40am UTC](https://forum.juce.com/t/read-access-violation-exception-on-audiobuffer/23035/1 "2017-06-23T08:40:11Z")

</div>

Hello!

I am trying to extract a part of an existing [AudioBuffer](https://www.juce.com/doc/classAudioBuffer) based on a [Range](https://www.juce.com/doc/classRange).

The array class I am using is `juce::Array<T>` , to avoid manual memory allocation.

```
	/* Range r-value version */      
	template <typename T>
	AudioBuffer<T> GetSubBuffer(AudioBuffer<T>& buffer, Range<int>&& range)
	{
		auto n = buffer.getNumChannels();

		Array<T*> raw;
		for (auto i = 0; i < n; i++) {
			raw.add(buffer.getWritePointer(i, range.getStart()));
		}
		AudioBuffer<T> sub(raw.getRawDataPointer(), n, range.getLength());

		return sub;
	}

```

Then I’m trying to use this method the following way:

```
	/* buf is a 480 samples buffer coming from the getNextAudioBlock() 
	 * method in an Audio Application. */
	auto sub = GetSubBuffer(buf, Range<int>(0, 100));
	sub.applyGain(0.01);

```

However, in the `applyGain` method, a read access violation exception is raised, coming from the `FloatVectorHelper::loadA(const Type* v)` method ( **v** is nullptr).

* * *

The call stack is quite straightforward and doesn’t help much:

```
Unit Tests.exe!juce::FloatVectorHelpers::BasicOps32::loadA(const float * v) Ligne 47	C++
Unit Tests.exe!juce::FloatVectorOperations::multiply(float * dest, float multiplier, int num) Ligne 760	C++
Unit Tests.exe!juce::AudioBuffer<float>::applyGain(int channel, int startSample, int numSamples, float gain) Ligne 590	C++
Unit Tests.exe!juce::AudioBuffer<float>::applyGain(int startSample, int numSamples, float gain) Ligne 600	C++
Unit Tests.exe!juce::AudioBuffer<float>::applyGain(float gain) Ligne 607	C++

```

When entering the `applyGain` method, `buffer` seems well defined:

```
juce::AudioBuffer<float>	{numChannels=2 size=100 allocatedBytes=0 ...}	juce::AudioBuffer<float>
    numChannels	2	int
    size	100	int
    allocatedBytes	0	unsigned __int64
    channels	0x000000fda96fe7e0 {0x0000024b01a8bb28 {0.000000000}}	float * *
    allocatedData	{data=0x0000000000000000 <NULL> }	juce::HeapBlock<char,1>
    preallocatedChannelSpace	0x000000fda96feb40 {0x0000024b01a8bb28 {0.000000000}, 0x0000024b01a8c328 {0.000000000}, 0x0000000000000000 {...}, ...}	float *[32]
    isClear	false	bool
    leakDetector1093	{...}	juce::LeakedObjectDetector<juce::AudioBuffer<float> >

```

* * *

It just means that I have a memory management problem somewhere, but I am not able to locate it.

Any help would be appreciated! 🙂

---

<div class="post-metadata">

**Author:** ![t0m](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/t0m/32/16413_2.png) [@t0m](https://forum.juce.com/u/t0m)\
**Post date:** [June 23, 2017, 9:47am UTC](https://forum.juce.com/t/read-access-violation-exception-on-audiobuffer/23035/2 "2017-06-23T09:47:53Z")

</div>

You could use a more appropriate `AudioBuffer` constructor. Try the following:

```
AudioBuffer<float> buf (2, 480);
Range<float> r (0, 100);
jassert (r.getEnd() < buf.getNumSamples());
AudioBuffer<float> sub (buf.getArrayOfWritePointers(),
                        buf.getNumChannels(),
                        r.getStart(),
                        r.getLength());
```

---

<div class="post-metadata">

**Author:** ![Cauchy](https://avatars.discourse-cdn.com/v4/letter/c/a88e57/32.png) [@Cauchy](https://forum.juce.com/u/Cauchy)\
**Post date:** [June 23, 2017, 11:17am UTC](https://forum.juce.com/t/read-access-violation-exception-on-audiobuffer/23035/3 "2017-06-23T11:17:09Z")

</div>

You’re totally right it’s simpler and it now works as expected, thank you. 🙂
