This should solve the memory leak reported in ticket #49.
This should solve the memory leak reported in ticket #49.

After trying several options, I found this to be the simplest solution
requiring the fewest changes that works in the most scenarios.

This solution replaces the internal `FITS_rec._coldefs` with
`FITS_rec.__coldefs` which may be a weak reference.  `FITS_rec._coldefs` is
now a property that either returns `FITS_rec.__coldefs` directly, or, if it's
a weakref, dereferences it and returns the hard reference.  Every assignment
to `FITS_rec._coldefs` that created a reference cycle has been replaced by
assigning `FITS_rec._coldefs` with a weak reference.  But it doesn't ''have''
to be a weakref either.  Making `FITS_rec._coldefs` into a property makes this
transparent.

Another possibility, rather than making `_coldefs` into a property, would have
been to use `weakref.proxy()`, but there was a scenario where this wouldn't
have worked:

In #44 I added support for a `FITS_rec.columns` attribute that gives users
more obvious access to the `ColDefs` object underlying a `FITS_rec` (some
users rightly stayed away from using `FITS_rec._coldefs` directly).  I wanted
to preserve this functionality.  Making `FITS_rec._coldefs` a weakref proxy
would have had suprising results for users.  For example.

{{{
#!python
>>> a = pyfits.new_table(...)
>>> c = a.data.columns  # I may want to do something with this later
>>> del a # I don't care about a, or I carelessly let it go out of scope
>>> print c
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ReferenceError: weakly-referenced object no longer exists
>>> # Oops!  Where did my ColDefs go?
}}}

If `FITS_rec._coldefs` were a proxy there would be no easy way to prevent
this, since I don't think it's possible to get a hard reference directly from
a proxy (without some sort of ugly hack).

So this solution is overall pretty transparent to both users and to most
internal code as well.



git-svn-id: 5305e2c1a78737cf7dd5f8f44e9bbbd00348fde7@844 ed100bfc-0583-0410-97f2-c26b58777a21

embray/PyFITS,embray/PyFITS,spacetelescope/PyFITS,embray/PyFITS,spacetelescope/PyFITS,embray/PyFITS