#14059: Fix refcount/deallocation of integers
-----------------------------+----------------------------------------------
       Reporter:  SimonKing  |         Owner:  rlm     
           Type:  defect     |        Status:  new     
       Priority:  blocker    |     Milestone:  sage-5.7
      Component:  memleak    |    Resolution:          
       Keywords:             |   Work issues:          
Report Upstream:  N/A        |     Reviewers:          
        Authors:             |     Merged in:          
   Dependencies:             |      Stopgaps:          
-----------------------------+----------------------------------------------

Comment (by SimonKing):

 Replying to [comment:12 jpflori]:
 > I think the point of this black magic is to keep a pool of pointers to
 Sage integers rather than allocating/freeing one each time a new one is
 needed/deleted.

 Yep, I understood that much.

 > The commented out line makes sense, inside the memory allocated for a
 Sage integer, we move to the address reserved for the mpz structure and
 initialize it, I guess its just illegal to cast to mpz_t type which is a
 typedef for an array of mpz of size one, something like mpz* should be
 used.

 So, you think one needs to write `mpz_init( <__mpz_struct*>(<char *>new +
 mpz_t_offset)`instead of `mpz_init( <mpz_t>(<char *>new + mpz_t_offset)`,
 and similar for deallocation?

 I tried
 {{{
 #!diff
 diff --git a/sage/rings/integer.pyx b/sage/rings/integer.pyx
 --- a/sage/rings/integer.pyx
 +++ b/sage/rings/integer.pyx
 @@ -5989,13 +5989,13 @@
          #
          # The clean version of the following line is:
          #
 -        #  mpz_init( <mpz_t>(<char *>new + mpz_t_offset) )
 +        mpz_init( <__mpz_struct*>(<char *>new + mpz_t_offset) )
          #
          # We save time both by avoiding an extra function call and
          # because the rest of the mpz struct was already initialized
          # fully using the memcpy above.

 -        (<__mpz_struct *>( <char *>new + mpz_t_offset) )._mp_d =
 <mp_ptr>mpz_alloc(GMP_LIMB_BITS >> 3)
 +        #(<__mpz_struct *>( <char *>new + mpz_t_offset) )._mp_d =
 <mp_ptr>mpz_alloc(GMP_LIMB_BITS >> 3)

      # This line is only needed if Python is compiled in debugging mode
      # './configure --with-pydebug' or SAGE_DEBUG=yes. If that is the
 @@ -6042,9 +6042,9 @@

      # Again, we move to the mpz_t and clear it. See above, why this is
 evil.
      # The clean version of this line would be:
 -    #   mpz_clear(<mpz_t>(<char *>o + mpz_t_offset))
 -
 -    mpz_free((<__mpz_struct *>( <char *>o + mpz_t_offset) )._mp_d, 0)
 +    mpz_clear(<__mpz_struct *>(<char *>o + mpz_t_offset))
 +
 +    #mpz_free((<__mpz_struct *>( <char *>o + mpz_t_offset) )._mp_d, 0)

      # Free the object. This assumes that Py_TPFLAGS_HAVE_GC is not
      # set. If it was set another free function would need to be
 }}}
 but it still segfaults at exit.

-- 
Ticket URL: <http://trac.sagemath.org/sage_trac/ticket/14059#comment:15>
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 unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sage-trac?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to