DAW changing buffer crashes


#1

Hello,
I have problem with my audio plugin in Logic Pro X (don’t know what about other DAWs, I don’t have other DAW to test).
The problem is: when my plugin is inserted on some channel in Logic everything is OK, until I change buffor size in Logic. I mean change buffor size, when Logic project is still open. It’s possible in Logic at all. But with my plugin everything crash, whole Logic just disappear, and have info about unexpectedly finish logic.
I suppose the problem is because I have in my processBlock dynamic allocation of pointer. Something like that:

filteredImpulse = new float[buffer.getNumSamples()];

I make it because I need to have access from other class to whole buffer (actually to each value in the buffer array). As I hope buffer is dependent from Logic Pro buffer, so that is why I want to update the length of array that holds the buffer values. I don’t know how to do that, so that is my solution.
Of course I delete it later, but I do that in the deconstructor of AudioProcessor. So I am also not sure if it’s good area to delete allocated memory.
I am even not sure if that the problem. But the main problem is, my DAW crashes when I change the buffer size. But when I close the Logic project, change the buffer size, and open project again with my plugin, there is everything OK. Could anyone help me?
Thanks in advance


#2

First thing first do not allocate any memory in processBlock() function unless and until absolutely required.
Secondly you might need to share more code so that we can say whats the problem.


#3

strike that part. You will have to find another solution!

Even if you have a limited buffer, Fabian had the great suggestion to wrap the processing in a way that in case of a big buffer, you can split it up into several internal calls with subsets of the buffer that your algorithm is capable of.

Also note, that there is no case known, when a host calls with a buffer bigger than the last one in prepareToPlay. If they do, they use their own API wrong, and would most likely fix that if brought to their attention.

That would usually not lead to a crash. What can happen (and will happen most likely) is that the audio thread will not return in time and you will hear bad noises and all kind of strange side effects.
If it crashes that means you are accessing bad memory, using an address that is being modified, hence pointing to random places, or anything else.
Impossible to guess without the complete code and easiest to do using a debugger.

When you tried that, you can post the block where the crash happens with the stack trace, than you will get the most precise answers.

Good luck!


#4

Of course I delete it later,

This is equivalent to what you’re doing:

float* oldArray = new float[10];
float* newArray = new float[12];
oldArray = newArray;
delete [] oldArray;

Spot the memory leak? You need to call delete[] before assigning the pointer returned by new to your variable.

Here’s a better way to do it.

//... in PluginAudioProcessor.h"
private:
    std::vector<float> filteredImpulse;

//... in PluginAudioProcessor.cpp
void prepareToPlay (double sampleRate, int maxBufferSize)
{
    //... rest of your initialization code
   filteredImpulse.resize (maxBufferSize);
}

The buffer size in processBlock will never be greater than the maximum buffer size set by the host in prepareToPlay, so worse case scenario is your array is slightly too large. prepareToPlay will be called when you change your buffer size and/or sampleRate. std::vector will handle all the memory management for you.

If it still is crashing, attach Xcode’s debugger to Logic and watch the plugin crash when you change the buffer size, then walk back through the stack trace to see why it happens.


#5

Hey,
Holy_Citys suggestion works perfect. Vector is the best solution for me now. No more crashes :slight_smile: super duper


#6

I still suspect you are doing something wrong.
Are you resizing your vector in processblock or in prepareToPlay function.
If you are doing in procesBlock you are still doing it wrong.


#7

Yes, I do it in prepareToPlay. Before I had dynamic allocation in processBlock, but now I know it’s bad :slight_smile:
But now I have similar problem with other plugin. This time no crashes, but not works properly. Now I have synthesizer plugin (so it’s midi plugin/instrument) and I want to make oscilator in the SynthesiserVoice class to be dependent on buffer size. I mean I want to have one impulse float 1.0f on each buffer block. So for example, if I have buffer size set to 512 and sample rate 44100, I want to receive 44100.0f / 512.0f impulses in the second.
So in my SynthesiserVoice class (actually myVoiceClass, which inherits from SynthesiserVoice) I created public int bufferSize. And In AudioProcessor::prepareToPlay I call:
myVoice->bufferSize = samplesPerBlock;

But it doesn’t update bufferSize. I am not sure what I am doing wrong. And I wonder maybe the problem is because myVoice is a pointer instead regular variable. Does it matter?