Lifetime of temporary objects in SWIG's Python wrappers (?)?

Second Update: Well we know that the basic problem is that python destroys Bar immediately. When Bar is implemented in python, python's gc knows that there's still a reference to theFoo, and so does not destroy it. But when Bar is implemented in c++, python calls the c++ destructor, which automatically destroys theFoo along with Bar.So the obvious solution is to prevent python from destroying Bar prematurely.

Here's a slightly hackish solution involving subclassing Bar: class PersistentBar(swigexample. Bar): lastpbar = None def __init__(self): super(PersistentBar, self). __init__() PersistentBar.

Lastpbar = self This saves a reference to the last Bar created so that it isn't destroyed right away. When a new Bar is created, the old one is deleted.(My old version was silly; no need to override __del__ for this.) Here's the output (with cout >> from test import persistentBar >>> persistentBar().theFoo. Value 1 >>> persistentBar().theFoo.

Value deleting Foo 1 >>> persistentBar().theFoo. Value deleting Foo 1 I still don't love this. It might be better to sequester the "persistent" behavior in a decorator; I tried that too and it worked (if you want to see the code let me know).

It would definitely be better to somehow tell python to handle destroying theFoo itself, but I can't figure out how to do that. First Update: The wrap code told me nothing, so I looked in swigexample.py. That also yielded nothing.

Things became clearer when I tried duplicating Bar in pure python: # pyfoobar.Py class Foo(object): def __init__(self): self. Value = -1 class Bar(object): def __init__(self): self. TheFoo = Foo() self.theFoo.

Value = 1 def __del__(self): self.theFoo. Value = 0 Now we import Bar from pyfoobar: >>> from pyfoobar import Bar >>> be = Bar() >>> b.theFoo. Value 1 >>> Bar().theFoo.

Value 0 This behavior is coming from Python! Original Answer: It seems like there's definitely some garbage collection combat at work here... Here's some related information on SWIG Memory Management. Based on this, it looks like the %newobject directive might be what you're looking for; but I tried several variations and couldn't get it to give python control over theFoo: >>> from swigexample import Bar >>> be = Bar() >>> b.theFoo.

Value 1 >>> b.theFoo. Thisown False >>> Bar().theFoo. Value 0 >>> Bar().theFoo.

Thisown False I'm beginning to suspect that this is intentional; seems like this line from the above link is relevant here: C is now holding a reference to the object---you probably don't want Python to destroy it. But I'm not certain. I'm going to look at the swigexample_wrap code to see if I can figure out when ~Bar is being called.

Certainly a good answer to the situation I posed. Unfortunately, it doesn't answer my real problem. I think I need to rework my original question some.

– Managu Feb 12 at 19:44 Ok, changed the question to better fit the real problem. – Managu Feb 12 at 19:56 I also tried writing my changed example up in pure python, following your example. In pure Python (i.e.

With a del method in Foo), Bar().theFoo. Value == 1, as expected. – Managu Feb 12 at 20:00.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions