I am trying to access to an existing C++ object from a python 3.4 extension. So I have the object coming from C++ with a SWIG binding. Then I am trying to build a C++ python extension where I would like to access to that object.
The reason I do that with python is because the library who create the object is an external library. So I use the python binding to generate objects and now I need to access to that object in C++.
To be more clear the object I use is Magick::Image
(from ImageMagick++) and I am trying to pass this object to a python extension and then convert the image to a numpy ndarray.
The difficulty here, it's I access to Magick::Image
object by a shared pointer.
So in python I have this:
img = get_my_magick_image()
print(img)
<Swig Object of type 'std::tr1::shared_ptr< Magick::Image > *' at 0x7f32f6ecdd80>
Here is the C++ extension I wrote to be able to access to Magick::Image
methods but I can only access to shard_ptr
methods... I got a segfault when I try to access to Magick::Image
methods. I use readimg(img)
in python to call it.
PyObject* readimg(PyObject* self, PyObject* args) {
std::tr1::shared_ptr< Magick::Image > * img;
if (!PyArg_ParseTuple(args, "O", &img)) {
PyErr_SetString(PyExc_TypeError, "error with parameters");
return 0;
}
std::cout << "img : " << img << std::endl;
std::cout << "&img : " << &img << std::endl;
std::cout << "typeid(img).name() : " << typeid(img).name() << std::endl;
std::cout << "typeid((*img)).name() : " << typeid((*img)).name() << std::endl;
std::cout << "img->use_count()" << img->use_count() << std::endl;
std::cout << "img->unique()" << img->unique() << std::endl;
std::cout << "img->get()" << img->get() << std::endl;
std::cout << "(*img).use_count()" << (*img).use_count() << std::endl;
std::cout << "(*img)" << (*img) << std::endl;
Magick::Geometry size = (*img)->size(); // SEGFAULT HERE
unsigned h = size.height();
std::cout << h << std::endl;
}
And the related output :
img : 0x7f32f6ecdd80
&img : 0x7fff280fbad8
typeid(img).name() : PNSt3tr110shared_ptrIN6Magick5ImageEEE
typeid((*img)).name() : NSt3tr110shared_ptrIN6Magick5ImageEEE
img->use_count() : 10296384
img->unique() : 0
img->get() : 0x2
(*img).use_count() : 10296384
(*img) : 0x2
Segmentation fault
Any idea why I can't access to Magick::Image
methods ?