Changeset 7123

Show
Ignore:
Timestamp:
01/30/10 02:56:30 (7 months ago)
Author:
dbaelde
Message:

Fix file descriptor leak in the new decoder detection plugins.
Thanks to Romain for spotting immediately this gross mistake!
I introduced Tutils.finalize for similar needs in the future.

Location:
trunk/liquidsoap/src
Files:
5 modified

Legend:

Unmodified
Added
Removed
  • trunk/liquidsoap/src/decoder/mp3.ml

    r7122 r7123  
    6565let get_type filename = 
    6666  let fd = Mad.openfile filename in 
    67     ignore(Mad.decode_frame_float fd); 
    68     let rate,channels,_ = Mad.get_output_format fd in 
    69       log#f 4 
    70         "Libmad recognizes %S as MP3 (%dHz,%d channels)." 
    71         filename rate channels ; 
    72       Mad.close fd ; 
    73       { Frame. 
    74           audio = channels ; 
    75           video = 0 ; 
    76           midi  = 0 } 
     67    Tutils.finalize ~k:(fun () -> Mad.close fd) 
     68      (fun () -> 
     69         ignore(Mad.decode_frame_float fd); 
     70         let rate,channels,_ = Mad.get_output_format fd in 
     71           log#f 4 
     72             "Libmad recognizes %S as MP3 (%dHz,%d channels)." 
     73             filename rate channels ; 
     74           { Frame. 
     75             audio = channels ; 
     76             video = 0 ; 
     77             midi  = 0 }) 
    7778 
    7879let () = 
  • trunk/liquidsoap/src/decoder/ogg_decoder.ml

    r7122 r7123  
    180180let get_type filename = 
    181181  let sync,fd = Ogg.Sync.create_from_file filename in 
    182   let decoder = Ogg_demuxer.init sync in 
    183   let feed ((buf,_),_) = 
    184     raise (Channels (Array.length buf)) 
    185   in 
    186   let audio = 
    187     try 
    188       Ogg_demuxer.decode_audio_rec decoder feed; 
    189       raise Not_found 
    190     with 
    191       | Channels x -> x 
    192       | Not_found  -> 0 
    193   in 
    194   let feed (buf,_) = 
    195     raise (Channels 1) 
    196   in 
    197   let video = 
    198     try 
    199       Ogg_demuxer.decode_video_rec decoder feed; 
    200       raise Not_found 
    201     with 
    202       | Channels x -> x 
    203       | Not_found  -> 0 
    204   in 
    205   let c_type = 
    206     { Frame. 
    207         audio = audio ; 
    208         video = video ; 
    209         midi  = 0 } 
    210   in 
    211     Unix.close fd ; 
    212     c_type 
     182    Tutils.finalize ~k:(fun () -> Unix.close fd) 
     183      (fun () -> 
     184         let decoder = Ogg_demuxer.init sync in 
     185         let feed ((buf,_),_) = 
     186           raise (Channels (Array.length buf)) 
     187         in 
     188         let audio = 
     189           try 
     190             Ogg_demuxer.decode_audio_rec decoder feed; 
     191             raise Not_found 
     192           with 
     193             | Channels x -> x 
     194             | Not_found  -> 0 
     195         in 
     196         let feed (buf,_) = 
     197           raise (Channels 1) 
     198         in 
     199         let video = 
     200           try 
     201             Ogg_demuxer.decode_video_rec decoder feed; 
     202             raise Not_found 
     203           with 
     204             | Channels x -> x 
     205             | Not_found  -> 0 
     206         in 
     207           { Frame. 
     208             audio = audio ; 
     209             video = video ; 
     210             midi  = 0 }) 
    213211 
    214212let () = 
  • trunk/liquidsoap/src/formats/wavformat.ml

    r7122 r7123  
    131131let get_type filename = 
    132132  let chan = open_in filename in 
    133   let info = Wav.read_header chan filename in 
    134     close_in chan ; 
    135     { Frame. video = 0 ; midi = 0 ; audio = Wav.channels info } 
     133    Tutils.finalize 
     134      ~k:(fun () -> close_in chan) 
     135      (fun () -> 
     136         { Frame. video = 0 ; midi = 0 ; 
     137                  audio = Wav.channels (Wav.read_header chan filename) }) 
    136138 
    137139let create_file_decoder filename kind = 
  • trunk/liquidsoap/src/tools/tutils.ml

    r7120 r7123  
    8181    with 
    8282      | e -> Mutex.unlock lock ; raise e 
     83 
     84let finalize ~k f = 
     85  try let x = f () in k () ; x with e -> k () ; raise e 
    8386 
    8487let seems_locked = 
  • trunk/liquidsoap/src/tools/tutils.mli

    r7120 r7123  
    6464val mutexify : Mutex.t -> ('a -> 'b) -> ('a -> 'b) 
    6565 
     66(** [finalize ~k f] calls [f] and returns it result, 
     67  * and always executes [k], even when [f] raises an exception. *) 
     68val finalize : k:(unit -> unit) -> (unit -> 'a) -> 'a 
     69 
    6670(** Tests whether a mutex is locked, without blocking. 
    6771  * We cannot check on Win32, where [true] is always returned: