1

I have post here ,a function that i use , to get the accelerator fft .

Setup the accelerator framework for fft on the iPhone

It is working great. The thing is, that i use it in real time, so for each new audio buffer i call this function with the new buffer.

I get a memory warning because of these lines (probably)

A.realp = (float *) malloc(nOver2 * sizeof(float));
 A.imagp = (float *) malloc(nOver2 * sizeof(float));

questions :

  • do i have another way, but to malloc them again and again(dont forget i have to feed it with a new buffer many times a second )

  • how exactly do i free them? (code lines)

  • can it caused by the fact that the fft is heavy to the system ?

Any way to get rid of this warning will help me a lot .

Thanks a lot.

Community
  • 1
  • 1
Curnelious
  • 1
  • 16
  • 76
  • 150

2 Answers2

3

These things should be done once, at the start of your program:

  • Allocate memory for buffers, using code like float *buffer = malloc(NumberOfElements * sizeof *buffer);.
  • Create an FFT setup, using code like FFTSetup setup = vDSP_create_fftsetup(log2n, FFT_RADIX2);.
  • Also test the return values. If malloc or vDSP_create_fftsetup returns 0, write an error message and exit the program or take other exception behavior.

These things should be done once, at the end of your program:

  • Destroy the FFT setup, using code like vDSP_destroy_fftsetup(setup);.
  • Release the memory for the buffers, using code like free(buffer);.

In the middle of your program, while you are processing samples, the code should use the existing buffers and setup. So the variables pointing to the buffers and the setup must be visible to that code. You can either pass them in as parameters (perhaps grouped together in a struct) or make them global (which should be only a temporary solution for small programs).

Your program should be arranged so that it is never necessary to allocate memory or create an FFT setup while samples are being processed.

All memory that is allocated should be freed eventually.

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312
  • so i have to put this : vDSP_create_fftsetup(log2n, FFT_RADIX2); at the start , but what about A.realp = (float *) malloc(nOver2 * sizeof(float)); ?? i do have to malloc A every time it gets the new buffer , so how can i remove it from the real time processing ? in other words, where should this malloc be ? – Curnelious Jan 03 '13 at 16:18
  • 1
    @Rant: Why do you write “I do have to malloc A every time it gets the new buffer”? You ought to allocate a buffer once and reuse it many times. What prevents you from doing that? You can fill the buffer with different data every time you get new samples. – Eric Postpischil Jan 03 '13 at 16:23
  • the buffer is not my problem , but &A . apple says he must be allocated . so i guess i will allocate it once at start- you are right. thank you very much . – Curnelious Jan 03 '13 at 16:52
  • Just want to add 'DSP_destroy_fftsetup(fftSetup)' is an incredibly useful line. Without it, the memory growth is huge and will quickly kill an iOS device. – Colin Feb 04 '14 at 12:04
  • @Colin: I hope you are not creating and destroy FFT setups repeatedly during your program. The intended way to use them is to create a setup once before it is needed, then to use it many times, then to destroy it after it will not be needed again (or at least not in the near future, while current tasks are continuing). – Eric Postpischil Feb 04 '14 at 14:10
2

If you are malloc'ing and never freeing, you will run out of memory. Make sure to 'free' your memory using free().

*Note: free() doesn't actually erase any memory. It simply tells the system that we're done with the memory and it's available for other allocations.

// Example:

// allocating memory
int *intpointer;
intpointer = malloc(sizeof(int));

// ... do stuff...

// 'Freeing' it when you are done
free(intpointer);
eric
  • 4,863
  • 11
  • 41
  • 55
  • thanks a lot i know that. i already freed it with free(A.realp); but it did not worked ... i guess you cant malloc and free so many times in 1 second . – Curnelious Jan 03 '13 at 14:38
  • 1
    @Rant: Calling malloc and free repeatedly with no accumulation of allocated memory will not cause any warning about memory. The only limit to how many you can do per second is how fast the routines execute. – Eric Postpischil Jan 03 '13 at 16:24