# Passing python vtk\_ptr to C++ Failed

**URL:** https://discourse.vtk.org/t/passing-python-vtk-ptr-to-c-failed/5713
**Category:** Support
**Created:** [May 6, 2021, 6:06am UTC](https://discourse.vtk.org/t/passing-python-vtk-ptr-to-c-failed/5713 "2021-05-06T06:06:31Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![WailmerPail](https://discourse.vtk.org/user_avatar/discourse.vtk.org/wailmerpail/32/3356_2.png) [@WailmerPail](https://discourse.vtk.org/u/WailmerPail)
#### Post date: [May 6, 2021, 6:06am UTC](https://discourse.vtk.org/t/passing-python-vtk-ptr-to-c-failed/5713/1 "2021-05-06T06:06:31Z")

</div>

Hello Team,

I tried to pass the py\_vtk\_ptr to cpp\_vtk for modifiying the value rapidly.  
I used the method from pletzer/vtkpythoncpp, like  
///====  
addr = int(pdata.GetAddressAsString(‘vtkPolyData’)[5:], 16)  
lib.foo(ctypes.c\_long(addr))  
//====

however, the first problem is, the address was clipped by “long”,  
python:Addr=00000261BF823AE0-\>2618589177776(int, 16)  
Cpp: FFFFFFFFB013E7B0

thus, I changed c\_long to c\_longlong, and the cpp function get the right pointer, but

when I called ptr-\>GetNumberofPoints(), it meets the error code: OSError: exception: access violation reading 0x0000000000000038.

I am using vtk 8.1(c++13), vtk-9.0(python 3.7), is that the key point?  
(I tried vtk-8.1 for python, it did not work too)  
Or in new version, there are other solution for that problems?

Thank you!

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.vtk.org/u/ben.boeckel)
#### Post date: [May 6, 2021, 12:54pm UTC](https://discourse.vtk.org/t/passing-python-vtk-ptr-to-c-failed/5713/2 "2021-05-06T12:54:55Z")

</div>

I think you want `ctypes.c_void_p`. On Windows, `c_long` is only 32bits which is where the truncation comes from. That said, I don’t know that this is a supported way to do such things. @dgobbi?

---

<div class="post-metadata">

### Author: ![dgobbi](https://discourse.vtk.org/user_avatar/discourse.vtk.org/dgobbi/32/18_2.png) [@dgobbi](https://discourse.vtk.org/u/dgobbi)
#### Post date: [May 6, 2021, 1:44pm UTC](https://discourse.vtk.org/t/passing-python-vtk-ptr-to-c-failed/5713/3 "2021-05-06T13:44:43Z")

</div>

I agree, use `ctypes.c_void_p`, since it’s the one designed for use with pointers of any type.

Either `pdata.GetAddressAsString('vtkPolyData')` or `pdata. __this__ ` will return a valid pointer for an object for any version of VTK. The `GetAddressAsString()` method is used in `vtk.tk.vtkTkRenderWidget`, which I have tested recently.

Edit: here is a comparison with the pointer shown by `PrintSelf()`:

```python
Python 3.8.5 (default, Jan 27 2021, 15:41:15) 
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from vtkmodules.vtkCommonCore import vtkVersion
>>> from vtkmodules.vtkCommonDataModel import vtkPolyData
>>> vtkVersion.GetVTKVersion()
'9.0.1'
>>> o = vtkPolyData()
>>> o.GetAddressAsString('vtkPolyData')
'Addr=0xbb5730'
>>> o. __this__'_0000000000bb5730_p_vtkPolyData'
>>> print(o)
vtkPolyData (0xbb5730)
  Debug: Off
  Modified Time: 28
  Reference Count: 1
  ....

```

---

<div class="post-metadata">

### Author: ![WailmerPail](https://discourse.vtk.org/user_avatar/discourse.vtk.org/wailmerpail/32/3356_2.png) [@WailmerPail](https://discourse.vtk.org/u/WailmerPail)
#### Post date: [May 7, 2021, 7:36am UTC](https://discourse.vtk.org/t/passing-python-vtk-ptr-to-c-failed/5713/4 "2021-05-07T07:36:51Z")

</div>

Sorry, but it failed again. I tried c\_void\_p in python, and fun(vtkPolyData\* ptr) and fun(void\* ptr) in c++.  
This is the error code:  
OSError: exception: access violation reading 0xFFFFFFFFFFFFFFFF

Logs before crash:  
\_0000022506c9c470\_p\_vtkPolyData  
python logs: addr : 0000022506C9C470  
C++ logs: 0000022506C9C470-\>0000022506C9C470

According to the logs, the c++ get the same ptr as python, but when I call it, it crashes.

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.vtk.org/u/ben.boeckel)
#### Post date: [May 7, 2021, 9:59am UTC](https://discourse.vtk.org/t/passing-python-vtk-ptr-to-c-failed/5713/5 "2021-05-07T09:59:39Z")

</div>

How are you obtaining the VTK Python module and how is `lib` compiled against that same VTK (if it isn’t, this is not going to work)?

---

<div class="post-metadata">

### Author: ![WailmerPail](https://discourse.vtk.org/user_avatar/discourse.vtk.org/wailmerpail/32/3356_2.png) [@WailmerPail](https://discourse.vtk.org/u/WailmerPail)
#### Post date: [May 7, 2021, 10:06am UTC](https://discourse.vtk.org/t/passing-python-vtk-ptr-to-c-failed/5713/6 "2021-05-07T10:06:12Z")

</div>

I complied the vtk-8.2 using Cmake+vs2017+Qt5 with default setting.  
python-vtk is installed by Pycharm-setting(maybe it is same as “pip install vtk-8.2”)

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.vtk.org/u/ben.boeckel)
#### Post date: [May 7, 2021, 3:39pm UTC](https://discourse.vtk.org/t/passing-python-vtk-ptr-to-c-failed/5713/7 "2021-05-07T15:39:10Z")

</div>

Yeah, those two builds are highly unlikely to be compatible with each other. You’ll need to compile the Python wrapping for the build that everything else is using because the `pip install vtk-8.2` comes with its own build of VTK. `VTK_WRAP_PYTHON` is the CMake flag that should work for 8.2 as well.
