[Python-Dev] reference leaks

Armin Rigo arigo at tunes.org
Fri Nov 25 09:59:55 CET 2005

Hi Neal,

On Thu, Nov 24, 2005 at 07:35:06PM -0800, Neal Norwitz wrote:
> The itertools.tee() fix I thought was quite obvious:
> +++ Modules/itertoolsmodule.c   (working copy)
> @@ -356,7 +356,8 @@
>  {
>         if (tdo->nextlink == NULL)
>                 tdo->nextlink = teedataobject_new(tdo->it);
> -       Py_INCREF(tdo->nextlink);
> +       else
> +               Py_INCREF(tdo->nextlink);
>         return tdo->nextlink;
>  }

No, if this object is saved as a cache on 'tdo' then obviously it needs
to keep a reference on its own.  This reference will go away in

After debugging, the problem is a reference cycle: the teedataobject
'head' has a field 'it' pointing to the generator-iterator '_fib()',
which has a reference back to 'head'.  So what is missing is making
teedataobject GC-aware, which it current isn't.

I suspect that there are other itertools types in the same situation.

A bientot,


More information about the Python-Dev mailing list