Excellent. Applied. Many thanks Doru!

Tim.

On Mon, Jun 12, 2006 at 04:52:44PM +0300, Doru Theodor Petrescu wrote:
> 
> Hi,
> 
> After a lot of hours I managed to trace a leak in the DBI module.
> The leak is quite small, and only affects long running programms that do
> a lot of queries. 
> 
> consider this short program:
> 
> 
> use DBI;
> $DB = DBI->connect( 'DBI:mysql:ttt', 'root', '' ) ;
> $DB->prepare("select 1") foreach ( 1..100000 );
> 
> it doesn't acctualy run anything. what it does it creates 100000s of
> statement handles and then destroys them. but this also leaks exactly 16
> bytes for each iteration. 
> 
> if you use Devel::LeakTrace module you'll notice that it complains about
> 100000s of leaked RVs from DBI.pm line 1188.
> on that line there is a call to _setup_handle() that is implemented in
> XS.
> 
> after more digging and a lot of trial and error, it looks like the
> problem is with the caching of ChildHandles.
> 
> what it actualy does it keeps a weak reference to each STH that it ever
> allocates. and every 120 or so, it does some house cleaning and remove
> from the array those references that are no longer valid.
> 
> the problem is that it NEVER actualy FREE those references. so they will
> leak.
> 
> the attached 2 line patch will add a sv_free() to free what is no longer
> needed.
> 
> 
> 
> Best Regards,
> Doru Theodor Petrescu
> 

> diff -uBbr orig/DBI-1.51/DBI.xs DBI-1.51/DBI.xs
> --- orig/DBI-1.51/DBI.xs      2006-06-06 12:49:13.000000000 +0300
> +++ DBI-1.51/DBI.xs   2006-06-12 16:26:52.000000000 +0300
> @@ -1069,6 +1069,8 @@
>                       SV *sv = av_shift(av);
>                       if (SvOK(sv))
>                           av_push(av, sv);
> +                     else 
> +                        sv_free(sv);         /* keep it leak-free by Doru 
> Petrescu [EMAIL PROTECTED] */
>                   }
>               }
>           }

Reply via email to