
I've been fiddling a bit with test_generators this morning, and have found that a stripped down version of the fibonacci test only leaks if the generator has a reference to a *copied* tee object. It doesn't matter whether the copied tee object is the second result from tee(), or if you just create a single tee object and use its __copy__() method, the leak only occurs if the cycle is: geniter -> frame -> ... -> copied_tee -> tdo ---+ ^ | | | +--------------------------------------------+ The "..." is to indicate that the frame may reference the object directly as a local variable, or via a cell. I've tried it both ways and it still leaks. Replacing "copied_tee" with an uncopied tee object does *not* leak. I have no idea what this means, although I've been staring at the relevant itertools code for some time now. It doesn't appear that the traverse functions are skipping anything. By the way, the above cycle will leak even if the generator is never iterated even once; it's quite simple to set up. I'm testing this using -R:: on test_generators, and hacking on the _fib function and friends.

At 12:53 PM 4/17/2006 -0400, Phillip J. Eby wrote:
By the way, the above cycle will leak even if the generator is never iterated even once; it's quite simple to set up. I'm testing this using -R:: on test_generators, and hacking on the _fib function and friends.
Follow-up note: it's possible to create the same leak with this code: l = [] a, b = tee(l) l.append(b) Which -R:: reports as leaking 4 references. If you "l.append(a)" instead of 'b', there is no leaking. This showed that the problem was actually in the itertools module, as no generators are involved here. After staring at tee_copy until my eyes bled, I accidentally scrolled such that tee_new was on the screen at the same time and notice that tee_copy was missing a call to PyObject_GC_Track();. So then I fixed everything up and tried to check it in, to find that Thomas Wouters already found and fixed this yesterday. The moral of the story? Always catch up on the Python-checkins list before trying to track down cycle leaks. :)
participants (1)
-
Phillip J. Eby