Read access violation exception on AudioBuffer



I am trying to extract a part of an existing AudioBuffer based on a Range.

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));

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! :slight_smile:


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(),


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