[C++-sig] Boost.Python and lifetime of instances of C++ classes

Hugo van der Merwe s13361562 at bach.sun.ac.za
Wed Jan 23 15:35:06 CET 2002


I've attached the source for a python extension (test.cpp), and a
test.py script, that uses it.

Here is the output of the test.py script:

==========
k = test.testclass():
test Constructor called.
k.printit():
HELLO!
l = test.foo()
l.add(k)
l.printit():
HELLO!
k = 0
test Destructor called.
l.printit():
HELLO!
==========

How does this work? Since "test Destructor called." appears, I'd think
k has been destroyed/deallocated. Then l.printit() calls k.printit(),
from C++. Something's odd, possibly even dangerous. Can someone explain
this?

Sorry if this does not compile under Windows - what's required, just the
"boilerplate" thingy added at the end of test.cpp?

Thanks,
Hugo
-------------- next part --------------
#!/usr/bin/env python1.5
import test

print "k = test.testclass():"
k = test.testclass()
print "k.printit():"
k.printit()
print "l = test.foo()"
l = test.foo()
print "l.add(k)"
l.add(k)
print "l.printit():"
l.printit()
print "k = 0"
k = 0
print "l.printit():"
l.printit()
-------------- next part --------------
A non-text attachment was scrubbed...
Name: test.cpp
Type: text/x-c++src
Size: 840 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20020123/7093191a/attachment.cpp>


More information about the Cplusplus-sig mailing list