[C++-sig] optimizing away calls to the python runtime -- was [detecting if a function is written in python or C++]

Mathieu Lacage Mathieu.Lacage at sophia.inria.fr
Fri Jan 13 15:19:02 CET 2006


On Fri, 2006-01-13 at 08:52 -0500, David Abrahams wrote:

[snip interesting answers to my questions]

> If the Python object is trying to deal with the event's lifetime
> management simultaneously, you're going to have trouble. AFAICT
> you haven't done anything to prevent the Python object from trying to
> manage the lifetime of the Event.

I thought the use of auto_ptr made python give up ownership of the
pointer and this is what my (rather limited) testing confirmed. Maybe
this is not the case which means that the double free will byte me and I
am screwed :/

This is what I did which, given some other comments below, is probably
what you suggest me to do.

void
simu_insert_in_s (double delta, std::auto_ptr<EventWrap> ev)
{
        Simulator::insert_in_s (delta, ev.get ());
        ev.release ();
}

[snip other interesting things]

> >   - python calls simulator.run which calls into Simulator::run
> >   - Simulator::run invokes c++ Event::notify which invokes back
> > Event.notify into python.
> >   - Event::notify deletes c++ object and should delete associated python
> > object.
> >
> > I fail to see how the end-user API I want to offer could be implemented
> > in another way without changing the wrapped c++ code.
> 
> Hold the EventWrap with an auto_ptr.  In its destructor, do

This, I do already. Maybe my original message was missing this.

> extract<auto_ptr<EventWrap>&>(self)().reset() before the

What I have currently is the same except for that call to reset which I
am not sure I understand. I suspect it is supposed to ask the auto_ptr
held by the wrapper to stop worrying about my object. If this is so, I
fail to see how my code could work without it because I should have had
a double free: one induced by my call to delete and the other induced by
the auto_ptr held by the python object when its last refcount is
released. Obviously, I don't have a double-free for now so... I am
probably just lucky.

I will look into this in more detail.

> Py_DECREF(self).  Then you've at least broken the ownership cycle,
> other evils notwithstanding.

understood.

[snip]

> Sorry, I don't have time right now to look into your case in that kind
> of detail. :(

Yes, I did not expect you to do my job for me but you cannot reproach me
trying to make you do it anyway :)

thanks a lot again for your thoughtful answers,
Mathieu
-- 




More information about the Cplusplus-sig mailing list