Skip to content

Copying of fftw_impl objects leads to double-free of FFTW plan pointers

The design of class FFT's constructor: FFT( const impl_type & impl=impl_type() , Flag flags=Default ) promotes the notion that impl_type should be copyable and copy-initializable, as it is always copy-initialized from the (defaulted) impl param. However, in the case of fftw_impl (the same for Intel MKL implementation), the impl holds a map of fftw_plan instances which should never be copied because they currently perform a 'naive' pointer copy, leading to double-free in case the copy is made. As a result FFT constructor is safe to call only in the case impl param holds empty map.

I believe the FFT plans should be made safe to copy (using eg. shared_ptr) or non-copyable so that any attempts to copy them would result in compile-time error.

I'm planning to provide merge request making them safe to copy.

repro case pseudo code

{
auto fft = Eigen::FFT<float>{};
// this causes that fft_impl inside fft stores a plan for src/nfft configuration
fft.fwd(dst, src, nfft);
{
// create another FFT op copying the cached FFT plan from `fft`
auto fftCopy = Eigen::FFT<float>{fft.impl()};
// fft_impl inside fftCopy will destroy/free  the plan at this point
}
// fft's destructor will perform second free on the same plan pointer here
}
Edited by Andrzej Ciarkowski