Re: __del__ not working with cyclic reference? (and memory-leaked?)

Discussion in 'Python' started by Jane Austine, Jul 2, 2003.

  1. Jane Austine

    Jane Austine Guest

    > Jane Austine wrote:
    >
    > > I read the gc module reference thoroughly and
    > > thanks to your short comment, I could understand it.
    > > (I should not use __del__ for singleton patterns, but
    > > any other alternatives?)

    >
    > I'm not sure why you'd want to use __del__ for a singleton pattern in
    > the first place, since singleton patterns are supposed to cushion the
    > creation and destruction of the singleton object away from the interface
    > user. What are you trying to do with singleton patterns as it relates
    > to __del__?


    For example, say the singleton has a database connection, in a cgi program.
    I want the singleton clear itself(and close the connection) before
    the whole process exits. It seems to be more convenient and
    object-oriented.

    [snip]
    >
    > However, Python doesn't make any guarantees about how timely __del__
    > will be called, or indeed even if it will be (in the case of circular
    > references). If you're finding yourself needing to rely on using


    Then "destructor" has less use than in other environments.
    Maybe I should avoid destructors in python as well as I can.

    > __del__ to get rid of important objects (such as those that are attached
    > to significant resources), a far better solution would be to use try:
    > ... finally: clauses surrounding the usage of such objects:
    >
    > resource = Resource()
    > try:
    > ... use resource ...
    > finally:
    > resource.close()
    >


    It is neat and reliable. Thanks.

    > --
    > Erik Max Francis && && http://www.alcyone.com/max/
    > __ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
    > / \ Men and women, women and men. It will never work.
    > \__/ Erica Jong
    Jane Austine, Jul 2, 2003
    #1
    1. Advertising

  2. Jane Austine wrote:

    > For example, say the singleton has a database connection, in a cgi
    > program.
    > I want the singleton clear itself(and close the connection) before
    > the whole process exits. It seems to be more convenient and
    > object-oriented.


    Typically singleton patterns need an out-of-band signal to tell them to
    destroy and reclaim the (protected) singleton instance. Usually it's
    done with some kind of 'close' or 'finish' method.

    Note that there are other patterns that are useful for singleton-like
    behaviors in Python; say, for instance, the Borg pattern (where each
    "instance" really just has a reference to the original instance's
    __dict__ contents).

    > Then "destructor" has less use than in other environments.
    > Maybe I should avoid destructors in python as well as I can.


    Not really. In C++, destructors are only called when an object is
    properly deleted. In Java, finalize methods have the same guarantees as
    Python's __del__ method; they're not guaranteed to be called in a timely
    fashion or at all.

    > It is neat and reliable. Thanks.


    Sure thing.

    --
    Erik Max Francis && && http://www.alcyone.com/max/
    __ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
    / \ When the solution is simple, God is answering.
    \__/ Albert Einstein
    Erik Max Francis, Jul 2, 2003
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jane Austine
    Replies:
    2
    Views:
    340
    Steven Taschuk
    Jul 2, 2003
  2. Christopher J. Bottaro

    super not working in __del__ ?

    Christopher J. Bottaro, Feb 15, 2005, in forum: Python
    Replies:
    0
    Views:
    316
    Christopher J. Bottaro
    Feb 15, 2005
  3. Fredrik Lundh

    Re: super not working in __del__ ?

    Fredrik Lundh, Feb 16, 2005, in forum: Python
    Replies:
    5
    Views:
    577
    Duncan Booth
    Feb 16, 2005
  4. Christopher J. Bottaro

    Re: super not working in __del__ ?

    Christopher J. Bottaro, Feb 16, 2005, in forum: Python
    Replies:
    5
    Views:
    765
    Jeff Shannon
    Feb 18, 2005
  5. AMLiapunov

    Scalars Leaked and Segmentation Fault

    AMLiapunov, Feb 21, 2006, in forum: Perl Misc
    Replies:
    1
    Views:
    108
    zentara
    Feb 23, 2006
Loading...

Share This Page