[C++-sig] how to correctly delete stuff 'extract<>'ed from python?

Achim H. achim-bpl at mol-net.com
Thu Jul 6 15:44:31 CEST 2006


The sad truth is: you have no direct control over the lifetime of *obj !

As long as somepyobj lives, *obj lives as well. One possibility is to set up a 
vector<boost::python::object> and put a copy of somepyobj (and probably more 
of the same) in there to keep up the reference count. Then, you only have to 
make sure that the vector is not (auto-)deleted before *obj is used for the 
last time. 

Or you do it the dirty way: Py_INCREF(somepyobj.ptr());

But a deliberate kill of *obj is impossible.

Achim.

Am Donnerstag, 6. Juli 2006 15:06 schrieb Kleistereimer:
> hi!
>
> how to correctly delete stuff  'extract<>'ed  from python?
> i have:
>
>  python::object somepyobj = ...
>  Tobj *obj = python::extract<Tobj*>(somepyobj);
>
>  obj->DoStuff(..);
>
>
> but the following line fails in debugmode:
>
>  delete obj;
>
>
> because of it was allocated from another heap. errormsg is:
>   _ASSERTE(_BLOCK_TYPE_IS_VALID(pHead->nBlockUse));
>   in dbgdel.cpp(52)
>
>
> in releasemode everything is fine. (i think this problem is silently
> ignored then)
>
>
> i guess i have to replace
>
>
>  delete obj;
> by something like:
>  python::delete_(obj);



More information about the Cplusplus-sig mailing list