473,396 Members | 1,755 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Does Boost.Python participate in cyclic gc?

We encountered a situation today where it appeared that a
Boost.Python-provided class didn't participate in Python's cyclic garbage
collection. The wrapped C++ instance held a reference to a method in the
Python object which referenced the Boostified C++ instance, e.g.:

class Foo:
def __init__(self, ...):
self.over_there = BoostifiedClass()
self.over_there.set_callback(self.boost_callback)
...

def boost_callback(self, ...):
...

When an instance of Foo went out of scope the BoostifiedClass instance it
referred to didn't disappear. (We could tell because it made connections to
another server which didn't disappear.) The solution was to recognize when
we where finished with it to set self.over_there to None. It would be nice
if Boost.Python knew how to play in the gc sandbox, but I suspect it may
not.

Thx,

Skip

Jun 5 '07 #1
2 1673
The solution was to recognize when we where finished with it to set self.over_there to None.

To my knowledge python does not handle all cyclic gc anyway. Here is
some information from the gc documentation:

[doc]
garbage
A list of objects which the collector found to be unreachable but
could not be freed (uncollectable objects). By default, this list
contains only objects with __del__() methods.3.1Objects that have
__del__() methods and are part of a reference cycle cause the entire
reference cycle to be uncollectable, including objects not necessarily
in the cycle but reachable only from it. Python doesn't collect such
cycles automatically because, in general, it isn't possible for Python
to guess a safe order in which to run the __del__() methods. If you
know a safe order, you can force the issue by examining the garbage
list, and explicitly breaking cycles due to your objects within the
list. Note that these objects are kept alive even so by virtue of
being in the garbage list, so they should be removed from garbage too.
For example, after breaking cycles, do del gc.garbage[:] to empty the
list. It's generally better to avoid the issue by not creating cycles
containing objects with __del__() methods, and garbage can be examined
in that case to verify that no such cycles are being created.
[/doc]

So, do BoostFieldClass and Foo both implement __del__? Is there some
way you can eliminate one of them?

I can reproduce something similar with the code below, and I'm not
using Boost.Python.

Expand|Select|Wrap|Line Numbers
  1. class Bar:
  2. def set_callback(self,cb):
  3. self.cb = cb
  4. def __del__(self):
  5. pass
  6.  
  7.  
  8. class Foo:
  9. def __init__(self):
  10. self.over_there = Bar()
  11. self.over_there.set_callback(self.boost_callback)
  12.  
  13. def boost_callback(self):
  14. print "Boost callback"
  15.  
  16. def __del__(self):
  17. pass
  18.  
  19. if __name__ == "__main__":
  20. import gc
  21. f = Foo()
  22.  
  23. del(f)
  24.  
  25. gc.collect()
  26.  
  27. print dir()
  28. print gc.garbage
  29.  
  30.  
Jun 5 '07 #2
On 05/06/2007 21.32, sk**@pobox.com wrote:
We encountered a situation today where it appeared that a
Boost.Python-provided class didn't participate in Python's cyclic garbage
collection. The wrapped C++ instance held a reference to a method in the
Python object which referenced the Boostified C++ instance, e.g.:
Those situations get more and more common as you keep wrapping your code. To
the best of my knowledge, SIP is the only one which does the right thing here.
--
Giovanni Bajo
Jun 7 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Barry Warsaw | last post by:
Python 2.3b2 is the second beta release of Python 2.3. There have be a slew of fixes since the first beta, and a few new "features". Our goal is to have a final Python 2.3 release by early...
0
by: Anthony Baxter | last post by:
On behalf of the Python development team and the Python community, I'm happy to announce the release of Python 2.3.3 (release candidate 1). Python 2.3.3 is a bug-fix release of Python 2.3. A...
0
by: Anthony Baxter | last post by:
On behalf of the Python development team and the Python community, I'm happy to announce the release of Python 2.3.3 (final). Python 2.3.3 is a bug-fix release of Python 2.3. A couple of serious...
0
by: Li Daobing | last post by:
I can't use .def(str(self)) I write a simple example, without `str', I can build it well, but with this one, I can't build //Rational.cpp #include <boost/python.hpp> #include <iostream> ...
0
by: Andrew Ayre | last post by:
Hi, I can't seem to get the library built, and any help is greatly appreciated. Here is the info: Windows XP Borland C++ Builder 5 Latest Boost source code (downloaded at the weekend) Windows...
11
by: Osiris | last post by:
I have these pieces of C-code (NOT C++ !!) I want to call from Python. I found Boost. I have MS Visual Studio 2005 with C++. is this the idea: I write the following C source file:...
0
by: Osiris | last post by:
My experiences with BOOST on Windows XP and Visual C++ 2005 I'm new to Python. I built software in more than ten other computer languages. I'm not sure if that is not a handicap, when reading...
4
by: Shawn McGrath | last post by:
Hi, I'm trying to expose a C++ class' internals to python via boost::python. I can do integer/boolean functions fine, but as soon as I do a string get/set it craps out. ...
0
by: devito | last post by:
hi there, for some days i try to build the boost.python tutorial "hello world" without bjam on winxp by using mingw. so i wrote a *.bat-file like the following: // --- snip...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.