What do I have to do in this case to prevent memory leakage for audio?

Hi,

I have like 40 sfx files that are getting played back randomly, I load them once and store their references and when I need to playback one of them, I pass it’s file reference to audio.play( handle, options ).

Questions are:

  1. Do I have to nullify the audio handles?

  2. Do I have to do anything to prevent memory leakage in this scenario?

I’ve read posts regarding memory leakage prevention with nullifying audio reference and whatnot but in my case I don’t want to lose references as I need to play them back again and again.

Thanks.

Hi Aidin,

Did you read Rob’s recent tutorial on “modulizing” audio? This may be the solution for you, to allow you to keep your audio loaded in a place where you can manage it easily.

http://www.coronalabs.com/blog/2013/06/04/tutorial-handling-cross-scene-audio/

And to answer your first question, audio files/handles must be disposed after you’re done using them (and you don’t need them again). This is explained in some detail in the Audio guide here:

http://docs.coronalabs.com/guide/media/audioSystem/index.html

Hope this helps,

Brent

Thanks Brent, I actually used that very tutorial to write my sfx manager.

My question is, do you have to manually nullify each audio handle that is created when you playback and audio or it’s disposed automatically?

Hi Aidin,

Yes, with audio you must manually dispose of loaded audio files/handles. This is why it’s convenient to localize them to a module or a scene-specific table where you can easily reference them and dispose/nil them when you’re done.

Brent

Thanks Brent,

But my original question still persists, let me rephrase it this way.

I have sound effects that I play back a lot in my game. I load them with “audio.loadSound” once and play them back with “audio.play”.

My question is, since each of my audio.play calls generate a handle, do I have to put an anonymous function to clear and nil it after it’s playback?

Thanks.

The handles are not allocated memory, it’s a fixed block of lua memory. When you play a 2nd time you write over the original block but it doesn’t use any more memory.

Thanks.

Hi Aidin,

Did you read Rob’s recent tutorial on “modulizing” audio? This may be the solution for you, to allow you to keep your audio loaded in a place where you can manage it easily.

http://www.coronalabs.com/blog/2013/06/04/tutorial-handling-cross-scene-audio/

And to answer your first question, audio files/handles must be disposed after you’re done using them (and you don’t need them again). This is explained in some detail in the Audio guide here:

http://docs.coronalabs.com/guide/media/audioSystem/index.html

Hope this helps,

Brent

Thanks Brent, I actually used that very tutorial to write my sfx manager.

My question is, do you have to manually nullify each audio handle that is created when you playback and audio or it’s disposed automatically?

Hi Aidin,

Yes, with audio you must manually dispose of loaded audio files/handles. This is why it’s convenient to localize them to a module or a scene-specific table where you can easily reference them and dispose/nil them when you’re done.

Brent

Thanks Brent,

But my original question still persists, let me rephrase it this way.

I have sound effects that I play back a lot in my game. I load them with “audio.loadSound” once and play them back with “audio.play”.

My question is, since each of my audio.play calls generate a handle, do I have to put an anonymous function to clear and nil it after it’s playback?

Thanks.

The handles are not allocated memory, it’s a fixed block of lua memory. When you play a 2nd time you write over the original block but it doesn’t use any more memory.

Thanks.