16 August 2009

BUG : NetStream/NetConnection memory leak

If you follow Flash Lite doc about Video.attachMovie, you should play a streamed FLV using this method

var my_video:Video; //my_video is a video object on stage
var my_nc:NetConnection = new NetConnection();
my_nc.connect(null);
var my_ns:NetStream = new NetStream(my_nc);
my_video.attachVideo(my_ns);
my_ns.play("video1.flv");


I just found a very annoying bug on this one :
when you call my_ns.play( filepath ), something is done on the background, then the video starts to stream & play when buffer is full
if you ever kill everything while 'something is done on the background', the 'something' doesn't stop and the first blocks of memory used by buffer are filled.

This is what I did for testing
var keyListener:Object = new Object( );
keyListener.onKeyDown = function()
{
handleKey(Key.getCode()+"" );
}
Key.addListener(keyListener);

function handleKey( keycode:String ):Void
{
switch (keycode)
{
case ExtendedKey.SOFT1:
trace(_root.stream_ns);
trace(_root.connection_nc);
break;
case ExtendedKey.SOFT2:
Key.removeListener(keyListener);

_root.stream_ns.close();
_root.connection_nc.close();
delete _root.stream_ns;
delete _root.connection_nc;
// nothing still present in memory

_root.gotoAndPlay("menu"); //get out of the frame with the video object
break;
}
}


Test this with a video (or download my sample)
Don't click the softkey yet, wait for the video to start playing
When you click the softkey, you could see on the memory manager every video data is deleted

Then retest, but quickly (or at least BEFORE the video start playing or you could see 'Connect to file_path' on the trace) click the softkey
At first, nothing happens (like you want to), you even have the wanted
FTPS116: Flash Video status : (NetConnection.Connect.Closed)
on the trace

BUT while the video is deconnected, closed, deleted and nulled, some (milli)seconds after you'll see a 'Connect to file_path' and the memory manager will show you something big on memory!

What happened?
I ask for a video
Flash Lite player starts something
I cancel the video and deleted it
Flash Lite doesn't know about it
Flash Lite finished its something
Flash Lite want to start the video but...err...where is my netstream ?! ok, forget it
Unfortunatly, it forget to kill its something

I fought a long time against this one, and I wasn't able to find a workaround....
The only thing I could do is to active the keylistener when the video is ready.
So no way to exit the screen before it starts playing...and so downloaded some Kbytes the user pay for! Very bad user experience !!
VGTrailers is like this, and I retried again this sunday to fix it without success...

I think it's NetStream/NetConnection bug. Not a Video bug, because if I don't use a video object and don't attachVideo, I have the same problem (and so I had sound but no video when I test)

If you ever got this one and found a solution, please, share it with me !

EDIT : I forgot to tell I tested it on read hardware, it's why you'll find oxygen's memchecker on the available test file

No comments: