Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #37087

Messing with the GC

From jt@toerring.de (Jens Thoms Toerring)
Newsgroups comp.lang.python
Subject Messing with the GC
Date 2013-01-19 14:47 +0000
Organization Freie Universitaet Berlin
Message-ID <alvpvkFthuoU1@mid.uni-berlin.de> (permalink)

Show all headers | View raw


Hi,

   triggered by some problems I had with PySide I got a bit
confused about what the GC may do in certain situations.
Here's a small test program I cobbled together:

import sys

class X( object ) :
    def __init__( self, parent, cnt ) :
        print( "In constructor for {0} {1}".format( self, cnt ),
               file = sys.stderr )
        self.parent = parent
        self.cnt = cnt

    def __del__( self ) :
        print( "In destructor for {0} {1}".format( self, self.cnt ),
               file = sys.stderr )

    def foo( self ) :
        print( "Before", file = sys.stderr )
        self.parent.z = X( self.parent, 2 )     # Is this bad?
        print( "After", file = sys.stderr )

class Y( object ) :
    def __init__( self ) :
        print( "In constructor for {0}".format( self ),
               file = sys.stderr )
        self.z = X( self, 1 )

    def __del__( self ) :
        print( "In destructor for {0}".format( self ),
               file = sys.stderr )

Y( ).z.foo( )

Have a look at the line with the comment. At this point the
only reference in existence to the X class instance, of which
a method is just being executed, goes out of scope. Thus I
would assume that the GC could now kick any time, possibly
even before the following call of print() or before the method
call returns. That, in turn might result in a crash of the
script.

Is my assumption about this flawed and there are no potential
dangers? Perhaps with

Y( ).z.foo( )

a temporary second reference is created that keeps the GC
for removing the X instance...

Another thing I'm puzzled about is the output of the
script:

In constructor for <__main__.Y object at 0x2919210>
In constructor for <__main__.X object at 0x2919310> 1
Before
In constructor for <__main__.X object at 0x2919350> 2
After
In destructor for <__main__.X object at 0x2919310> 1

Ok, the destrucor for the first instance of the X class is
called only after printing out "After", so the GC didn't
delete the object before. But then there are obviously no
calls of the destructors of neither the second instance
of the X class nor of the Y class instance. Shouldn't
they be invoked before the program ends?

                Thanks and best regards, Jens
-- 
  \   Jens Thoms Toerring  ___      jt@toerring.de
   \__________________________      http://toerring.de

Back to comp.lang.python | Previous | NextNext in thread | Find similar | Unroll thread


Thread

Messing with the GC jt@toerring.de (Jens Thoms Toerring) - 2013-01-19 14:47 +0000
  Re: Messing with the GC Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-01-19 16:24 +0000
    Re: Messing with the GC Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-01-19 16:34 +0000
  Re: Messing with the GC Terry Reedy <tjreedy@udel.edu> - 2013-01-19 11:40 -0500
  Re: Messing with the GC Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-01-19 16:44 +0000
  Re: Messing with the GC jt@toerring.de (Jens Thoms Toerring) - 2013-01-20 20:09 +0000
    Re: Messing with the GC Terry Reedy <tjreedy@udel.edu> - 2013-01-20 17:14 -0500
      Re: Messing with the GC jt@toerring.de (Jens Thoms Toerring) - 2013-01-21 01:02 +0000

csiph-web