#12313: Fix yet another memory leak caused by caching of coercion data
--------------------------------------------------+-------------------------
       Reporter:  SimonKing                       |         Owner:              
                             
           Type:  defect                          |        Status:  
needs_review                             
       Priority:  major                           |     Milestone:  sage-5.3    
                             
      Component:  memleak                         |    Resolution:              
                             
       Keywords:  coercion weak dictionary        |   Work issues:              
                             
Report Upstream:  N/A                             |     Reviewers:  Simon King, 
Jean-Pierre Flori, John Perry
        Authors:  Simon King, Jean-Pierre Flori   |     Merged in:              
                             
   Dependencies:  #11521, #11599, #12969, #12215  |      Stopgaps:              
                             
--------------------------------------------------+-------------------------

Comment (by nbruin):

 I think two dictionaries are to blame: `_cache` in
 `sage/rings/polynomial/polynomial_ring_constructor.py` and `self._cache`
 in `UniqueFactory`. If I change both to `dict()` I don't get an error. If
 either is `Weak`, I do get the error.

 In the process I saw how precarious `remove` methods are. I wanted to put
 a `sys.stderr.write` in the `remove` function in `WeakValueDictionary`. I
 ended up with
 {{{
     def __init__(self, *args, **kw):
         MYSTDERR=sys.stderr
         def remove(wr, selfref=ref(self)):
             self = selfref()
             if self is not None:
                 MYSTDERR.write("REMOVING A KEY\n");
                 del self.data[wr.key]
         self._remove = remove
         UserDict.UserDict.__init__(self, *args, **kw)
 }}}
 (so `remove` is a closure that keeps a reference to `MYSTDERR`) because
 anything less would lead to errors. Apparently you cannot assume that the
 globals dictionary of your module is still intact when your remove method
 runs. That is such a restriction that I think we cannot expect people to
 write their equality testers to survive in such a harsh environment. We
 either have to track down all caches and tear them down in a more friendly
 setting or we have to go with a "safe" WeakValueDictionary, which doesn't
 call methods on keys upon removal.

 I'm learning much more about Python than I ever wanted to know ...

-- 
Ticket URL: <http://trac.sagemath.org/sage_trac/ticket/12313#comment:217>
Sage <http://www.sagemath.org>
Sage: Creating a Viable Open Source Alternative to Magma, Maple, Mathematica, 
and MATLAB

-- 
You received this message because you are subscribed to the Google Groups 
"sage-trac" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/sage-trac?hl=en.

Reply via email to