From fa47b7b1367721a2e83f21d1bfcf943116e3d7d1 Mon Sep 17 00:00:00 2001 From: "Steinar H. Gunderson" Date: Sun, 9 Dec 2007 16:34:37 +0100 Subject: [PATCH] "Fix" a memory leak. --- foosrank.cpp | 46 +++++++++++++++++++++++++--------------------- 1 file changed, 25 insertions(+), 21 deletions(-) diff --git a/foosrank.cpp b/foosrank.cpp index b9208fd..03edcb5 100644 --- a/foosrank.cpp +++ b/foosrank.cpp @@ -106,27 +106,31 @@ static void compute_opponent_rating_pdf(int k, int a, double mu2, double sigma2, int sz = (6000.0 - 0.0) / int_step_size; double h = (6000.0 - 0.0) / sz; - fftw_plan f1, f2, b; - complex *func1, *func2, *res; - - func1 = reinterpret_cast *>(fftw_malloc(sz*2*sizeof(complex))); - func2 = reinterpret_cast *>(fftw_malloc(sz*2*sizeof(complex))); - res = reinterpret_cast *>(fftw_malloc(sz*2*sizeof(complex))); - f1 = fftw_plan_dft_1d(sz*2, - reinterpret_cast(func1), - reinterpret_cast(func1), - FFTW_FORWARD, - FFTW_MEASURE); - f2 = fftw_plan_dft_1d(sz*2, - reinterpret_cast(func2), - reinterpret_cast(func2), - FFTW_FORWARD, - FFTW_MEASURE); - b = fftw_plan_dft_1d(sz*2, - reinterpret_cast(res), - reinterpret_cast(res), - FFTW_BACKWARD, - FFTW_MEASURE); + static bool inited = false; + static fftw_plan f1, f2, b; + static complex *func1, *func2, *res; + + if (!inited) { + func1 = reinterpret_cast *>(fftw_malloc(sz*2*sizeof(complex))); + func2 = reinterpret_cast *>(fftw_malloc(sz*2*sizeof(complex))); + res = reinterpret_cast *>(fftw_malloc(sz*2*sizeof(complex))); + f1 = fftw_plan_dft_1d(sz*2, + reinterpret_cast(func1), + reinterpret_cast(func1), + FFTW_FORWARD, + FFTW_MEASURE); + f2 = fftw_plan_dft_1d(sz*2, + reinterpret_cast(func2), + reinterpret_cast(func2), + FFTW_FORWARD, + FFTW_MEASURE); + b = fftw_plan_dft_1d(sz*2, + reinterpret_cast(res), + reinterpret_cast(res), + FFTW_BACKWARD, + FFTW_MEASURE); + inited = true; + } // start off by zero for (int i = 0; i < sz*2; ++i) { -- 2.39.2