[vtkusers] Questions About Python Wrapping

Kyle Sunderland 1krs1 at queensu.ca
Thu May 24 12:14:10 EDT 2018


Hi David,


When wrapping classes, does the wrapping rely on the "vtk" prefix for both filenames and class names?


I imagine that renaming the classes would fix the issue, but are there any other workarounds for wrapping classes that begin with a different prefix, or to wrap functions using these classes as arguments?

Is it possible to specify a prefix other than "vtk" for use with the wrapper?


Thanks again for your help!

- Kyle

________________________________
From: Andras Lasso
Sent: May 17, 2018 9:05:36 PM
To: David Gobbi
Cc: Kyle Sunderland; vtkusers at vtk.org
Subject: RE: [vtkusers] Questions About Python Wrapping


VTK_NEWINSTANCE hint is great! For Python, it solves the returned object ownership issue completely in a nice and robust way (now we just need to figure out how to update all our code base to use this).



For C++, a new smart pointer type (vtkSafePointer<…>?) could be added that is exclusively used for returning new instances. This smart pointer type would not have explicit converter to raw pointer but could be directly assigned to vtkSmartPointer<…>. It could also have a helper method for backward compatibility, which would increment reference count and return a raw pointer. This new pointer type could be easily introduced for new classes, but for existing classes it would require small change in all code where new VTK object instances are returned, which developers would probably not like. Maybe it could be done when changes around this are required anyway for some other reasons.



Andras



From: David Gobbi <david.gobbi at gmail.com>
Sent: Thursday, May 17, 2018 7:04 PM
To: Andras Lasso <lasso at queensu.ca>
Cc: Kyle Sunderland <1krs1 at queensu.ca>; vtkusers at vtk.org
Subject: Re: [vtkusers] Questions About Python Wrapping



The vtkSmartPointer isn't very safe in this regard. It is easy to accidentally do the following, since VTK smart pointers implicitly convert to raw pointers:



  vtkSmartPointer<vtkObject> MyMethod();

  vtkObject *obj = MyMethod(); // ownership is not caught, object is autodeleted



The preferred way of dealing with transfer of ownership for returned values in VTK is to add the VTK_NEWINSTANCE hint to the return value.  Then the wrappers will automatically handle the reference count.  As for people who use these methods from C++, they still have to worry about memory leaks, but at least they don't have to worry about memory corruption caused by mistakes like the one in the example above.



 - David





On Thu, May 17, 2018 at 4:29 PM, Andras Lasso <lasso at queensu.ca<mailto:lasso at queensu.ca>> wrote:

The main advantage of the ability to return smart pointers would be that ownership of VTK objects could be transferred safely to the caller, without requiring calling UnRegister(None). C++ developers are aware of pointers but for Python users it is a mistery why sometimes they need to call UnRegister to avoid memory leaks.



Andras

________________________________

From: David Gobbi <david.gobbi at gmail.com<mailto:david.gobbi at gmail.com>>
Sent: Thursday, May 17, 2018 4:55 PM
To: Kyle Sunderland
Cc: vtkusers at vtk.org<mailto:vtkusers at vtk.org>
Subject: Re: [vtkusers] Questions About Python Wrapping



Hi Kyle,



Thanks for sending the files.  I see that the parameters for those methods are actually smart pointers, and as you have already noted, smart pointers aren't wrapped.



This has never been an issue in VTK proper or for most VTK projects, since VTK methods generally use raw pointers.  The header file you sent is closer to ITK style than to VTK style.



I definitely think that it would be worthwhile to add better smart pointer handling to the wrappers.



 - David





On Thu, May 17, 2018 at 12:13 PM, David Gobbi <david.gobbi at gmail.com<mailto:david.gobbi at gmail.com>> wrote:

On Thu, May 17, 2018 at 10:10 AM, Kyle Sunderland <1krs1 at queensu.ca<mailto:1krs1 at queensu.ca>> wrote:
> It (mostly) works, however I noticed that no functions are wrapped which have a definition that contains any of the wrapped classes. Does anyone have insight into what I might have missed?

Is a vtk..Hierarchy.txt file generated for the wrapped module when you build it?  If so, send it to me along with the header files you're wrapping, and I can take a look.

> Also, as an additional question, is it still not possible to wrap functions that return a smart pointer?

If this is a feature that you need, then add it to the bug tracker.



Cheers,

 - David




-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://vtk.org/pipermail/vtkusers/attachments/20180524/beffef65/attachment.html>


More information about the vtkusers mailing list