# Memory leaks – eventlisteners and new sounds

**URL:** https://forum.kirupa.com/t/memory-leaks-eventlisteners-and-new-sounds/314962
**Category:** Uncategorized
**Created:** [October 13, 2010, 1:59pm UTC](https://forum.kirupa.com/t/memory-leaks-eventlisteners-and-new-sounds/314962 "2010-10-13T13:59:40Z")
**Posts on this page:** 1
**Page:** 1

<div class="post-metadata">

### Author: ![jaxz](https://avatars.discourse-cdn.com/v4/letter/j/c6cbf5/32.png) [@jaxz](https://forum.kirupa.com/u/jaxz)
#### Post date: [October 13, 2010, 1:59pm UTC](https://forum.kirupa.com/t/memory-leaks-eventlisteners-and-new-sounds/314962/1 "2010-10-13T13:59:40Z")

</div>

**Memory leaks – eventlisteners and new sounds.**

Dear all,  
I have written up a media player code, which does what it is supposed to do, but the System.totalMemory steadily increases with time.  
My code essentially consists of nested calls using EvenListeners that trigger initiation of new sounds.  
I call up mp3 files that load and start play and on EventCOMPLETE load the next file  
Example of code snippet:

```auto

function **LoadNextTheme** (e:Event = null):void // set next theme
{
	if (A_play) {
		nextA = new Sound();
		nextA.addEventListener(Event.COMPLETE, **PlayNextTheme** );
		nextA.load(gettheme);		
	}
	else {
		nextB = new Sound();
		nextB.addEventListener(Event.COMPLETE, **PlayNextTheme** );
		nextB.load(gettheme);		
	}	
}

function **PlayNextTheme** (e:Event):void	
{
		if (A_play) {
			ThemeLong = nextA.length; 
			if (SC_A != null) {SC_A.stop();} SC_A = nextA.play();		
		}
		else {
			ThemeLong = nextB.length; 
			if (SC_B != null) {SC_B.stop();} SC_B = nextB.play();			
			SC_B.soundTransform = muteform_AB;						
		}
	var timer_AB: Timer = new Timer(ThemeLong - dt,1);	
	if (special conditions met) {
		timer_AB.addEventListener(TimerEvent.TIMER, **LoadNextTheme** ); 
		timer_AB.start();	 
}

```

What parts of this kind of code keeps adding to total systemmemory?  
And how can I best stop that leak?  
Please advice,  
Jaxz
