Fixes memleak and should fix issue791. Originally committed as revision 18629 to svn://svn.ffmpeg.org/ffmpeg/trunk