View Full Version : Video player memory leak help?

11-11-2010, 03:39 PM
Hey guys,

I'm trying to create a video player app which gets its playlist from an external xml and its going well but I'm getting some serious memory leaks.

Here is my xml playlist:

<?xml version="1.0" encoding="utf-8"?>
<mp4 label="TestMovie1.mp4" icon="myMovieIcon" />
<mp4 label="TestMovie2.mp4" icon="myMovieIcon" />
<mp4 label="TestMovie3.mp4" icon="myMovieIcon" />
<mp4 label="TestMovie4.mp4" icon="myMovieIcon" />

My main code:

import flash.events.Event;

public var playlist:String; //"region1"

public var currentItem:Number = 0;

private var dataXML:XML;
private var contentList:Array = new Array();
private var workDir:String = "c:/temp/";

private function loadXml():void
var loader:URLLoader = new URLLoader(new URLRequest(workDir + playlist + ".xml"));
loader.addEventListener("complete", parseXml);

private function parseXml(e:Event):void
dataXML = XML(e.target.data);
var listLength:Number = dataXML.children().length();
for(var i:Number = 0; i<listLength; i++)


private function nextItem():void
var dataFile:File = new File(workDir + contentList[currentItem]);

if(dataFile.extension == "mp4" || dataFile.extension == "flv")
var vp:videoPlayer = new videoPlayer();

vp.addEventListener(Event.COMPLETE, movieDone);
vp.x = 0;
vp.y = 0;
vp.width = this.width;
vp.height = this.height;
vp.vidName = workDir + contentList[currentItem];
vp.vidWidth = vp.width;
vp.vidHeight = vp.height;
vp.vidDis.scaleMode = "letterbox";
trace(contentList[currentItem] + " can't be located");


public function movieDone(e:Event):void

if(currentItem == contentList.length -1)
currentItem = 0;
currentItem = currentItem + 1;

And the videoPlayer component:


[Event(name="complete", type="flash.events.Event")]


import org.osmf.events.TimeEvent;

public var vidName:String;
public var vidWidth:Number;
public var vidHeight:Number;

protected function videoplayer1_completeHandler(event:TimeEvent):void
var e:Event = new Event("complete");

<mx:Container x="0" y="0" width="{vidWidth}" height="{vidHeight}" backgroundColor="#000000">
<s:VideoDisplay id="vidDis" x="0" y="0" width="{vidWidth}" height="{vidHeight}" source="{vidName}" complete="videoplayer1_completeHandler(event)" chromeColor="#000000"/>

So I create the videoPlayer component and pass in the mp4 file from an array. Once the video is complete it dispatches a custom event which runs the movieDone function. I call the this.removeAllElements() to remove the videoPlayer component before adding it again. I am very new at this but it doesnt seem like the video is being removed as the physical memory climbs with each new video, and eventually after several loops the program will crash. Anyone know what I'm doing wrong?

11-11-2010, 07:24 PM
what about the Array are you clearing that?


wait, I think you should be able to do it without re-instantiating the videoPlayer every time. That's what is building up, even when you remove it, you need to only call
new videoPlayer(); once, preferably in a constructor.

04-21-2011, 12:55 PM
I think you cant load file directly (sandbox limitation) from the file system, except when creating a AIR application.

04-22-2011, 01:12 AM
I 2nd Tadster. You want to swap your video source, not your video player.

04-29-2011, 11:15 AM
I don't think you can memory leak in actionscript

04-29-2011, 02:48 PM
I don't think you can memory leak in actionscript
You most certainly can. Any object that still has a reference to it will never be garbage collected. The most common cause of this is failing to remove event listeners.