[C++-sig] Creating a dictionary to pass to python
Stefan Seefeld
seefeld at sympatico.ca
Sat Jan 13 22:41:03 CET 2007
David Sveningsson wrote:
> At the moment I use this code:
>
> PyObject* getDict(){
> PyObject* m = PyDict_New();
>
> PyDict_SetItem(m, PyString_FromString("test"), PyFloat_FromDouble(4.6f);
>
> return m;
> }
>
> And have exported it like this:
>
> def("getDict", &getDict);
>
> But this seems to leak some memory. I guess I should use
> manage_new_object as a return policy but when I do I get a segfault. It
> seems like it tries to use free but should use delete instead.
>
> Is this the "right" way or should I do it some other way? What about
> memory leakage?
namespace bpl = boost::python;
bpl::dict getDict()
{
bpl::dict d;
d["test"] = 4.6;
return d;
}
def("getDict", getDict);
There is no reason to use the C API, and to have to worry about memory management.
HTH,
Stefan
--
...ich hab' noch einen Koffer in Berlin...
More information about the Cplusplus-sig
mailing list