Weird interaction between FFTW and AudioBuffer


I’m trying to create my own STFT class using FFTW to compute the FFT of an AudioBuffer. I spotted a weird behaviour with FFTW which seems to interact in a weird way with my AudioBuffer.

Here’s the code i’m using :

The STFT Class :

class STFT
    STFT(juce::AudioBuffer<float>* x, unsigned int windowSize): signal(x), windowLength(windowSize){}; 
    void computeSTFT();
    juce::AudioBuffer<float>* signal;
    int windowLength;

The compute_STFT function

void STFT::computeSTFT()
    std::cout<<"in "<<signal->getNumSamples()<<"\n";
    float* analysisFrame;
    std::complex<float>* fftOut = new std::complex<float>[(int) (windowLength/2 + 1)];
    fftwf_plan plan;
    std::cout<<"mid "<<signal->getNumSamples()<<"\n";
    plan = fftwf_plan_dft_r2c_1d(windowLength, analysisFrame, reinterpret_cast<fftwf_complex*>(fftOut), FFTW_MEASURE); 
    std::cout<<"mid2 "<<signal->getNumSamples()<<"\n";
    delete fftOut;
    std::cout<<"out "<<signal->getNumSamples()<<"\n";

This is what is printed in the terminal :

in 8865
mid 8865
mid2 0
out 0

So for some reason something happens to the AudioBuffer pointer when creating the fftw plan even though the AudioBuffer is not given as an argument to fftw’s plan creation function
Anyone know what is happening?

You are not allocating (or even setting to null pointer) the analysisFrame buffer but you are giving it as an argument for the fftwf_plan_dft_r2c_1d function. My guess is that the fftw function is then overwriting some “random” memory in your program, causing the AudioBuffer to misbehave. (The fftw documentation suggests that the memory will not be overwritten if FFTW_MEASURE is used, but that might be some kind of mistake, and in any case the docs also say that the passed in buffer must be allocated.)

You’re right! Allocating analysisFrame made it work.
Thank you