[C++-sig] Boost.Python: Callbacks to class functions

Jim Bosch talljimbo at gmail.com
Tue Aug 2 07:24:04 CEST 2011

On 08/01/2011 09:44 PM, diego_pmc wrote:
>     Are these cycles actually a problem in practice?  Python does do
>     garbage
>     collection, so it might be that it knows about all these dependencies
>     and just hasn't bothered to try to delete them because it doesn't need
>     the memory yet.
>   Yes, they are problematic. When the object gets removed it should not
> receive any more events or it will very likely result in some odd
> behavior on the screen. These objects are entity states in a game. A
> state dictates how an entity reacts to game events and when the entity
> changes its state, the old one should stop giving the entity
> instructions, or they will conflict with the instructions given by the
> new state.

Hmm.  That might mean you need to do a big design change; while it often 
works, one really isn't supposed to rely on __del__ being called when a 
Python object first could be garbage-collected - when cycles are 
involved, Python doesn't even guarantee that it will ever call __del__. 
  It sounds like you'd be much better off with a named destructor-like 
method that would be called explicitly when you want to remove an object 
from the game.


More information about the Cplusplus-sig mailing list