bug?: exception and references 
Author Message
 bug?: exception and references

Hi,

I have a problem with exceptions incrementing the reference counter of an
instance whose method is raising the exception. To illustrate the problem
here's an example completely written in Python:

We have two methods in RaiseTest, func1 raising an exception on error, and
func2 returning a -1 value. If an instance of this class is created, and the
function func1 is called, freeing up the instance is delayed. If func2 is
called everything works normally.

class RaiseTest:

        def __del__(self):
                print "__del__ called"

        def func1(self):
                raise NotImplementedError

        def func2(self):
                return -1

def test1():
        print "test1"

        r = RaiseTest()
        try:
                r.func1()
        except NotImplementedError:
                pass
        del r
        print "r deleted1"

def test2():
        print "test2"

        r = RaiseTest()
        r.func2()
        del r
        print "r deleted2"

test1()
test2()

Results of the script:

balabit:~/src$ python proba.py
test1
r deleted1
__del__ called
test2
__del__ called
r deleted2

The order of messages is important. In test1, the instance is deleted using
"del", but is not freed until the current frame exits (this time test1), in
test2, the instance is correctly freed when "del" is called.

Something thus holds a reference to the instance, even if it's deleted from
the local namespace of test1(), my question is what it might be?

I'm using Python as an extension language, and I call the method using
PyObject_CallObject(), if an exception is raised in the function, nothing
frees up the object instance (because of the extra reference).

Any help is welcome.

PS: please CC me as I'm not on the list.

--
Bazsi
PGP info: KeyID 9AF8D0A9 Fingerprint CD27 CFB0 802C 0944 9CFD 804E C82C 8EB1
     url: http://www.*-*-*.com/



Fri, 04 Jul 2003 22:10:03 GMT  
 bug?: exception and references

Quote:

> Something thus holds a reference to the instance, even if it's deleted from
> the local namespace of test1(), my question is what it might be?

Something starting from sys.last_traceback, I'd think.

Quote:
> I'm using Python as an extension language, and I call the method using
> PyObject_CallObject(), if an exception is raised in the function, nothing
> frees up the object instance (because of the extra reference).

It will get freed up eventually (ie. when the next exception gets
raised).  Or you could assign None to sys.last_traceback, or "del"
that (I'm not sure what best practice is here).

Cheers,
M.

--
  If comp.lang.lisp *is* what vendors are relying on to make or break
  Lisp sales, that's more likely the problem than is the effect of
  any one of us on such a flimsy marketing strategy...
                                      -- Kent M Pitman, comp.lang.lisp



Fri, 04 Jul 2003 23:36:58 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. BUG: exception in exception handler

2. References in Ada exceptions

3. __del__, exceptions and reference counting: an evil trio

4. BUGS, BUGS, BUGS, BUGS, C4 BUGS

5. Bug: #squared explain it returns exception

6. Bug: $a explain it returns exception

7. Exception handling bug?

8. FloatDomainError exception, bug in Ruby?

9. Valid on QNaN generates exception, compiler bug?

10. Bug in Python 1.5.2 exception handling?

11. Doc-string bug in exceptions.py

12. unreported exception - bug in 1.3?

 

 
Powered by phpBB® Forum Software