Pointer to member memory management in Python

Pointer to member memory management in Python

When a function returns a pointer/reference to a member, we don't want the owning object to get cleaned up while the managed code still holds the wrapper of the pointer. For C#, the following strategy is given in the docs:

Re: Pointer to member memory management in Python

Although undocumented as far as I can tell, this causes the wrapped value to be stored in a local variable called "val", so the equivalent of the C# example becomes:

%pythonappend getWheel %{

val._bike = self

%}

I think it would be worth updating the docs to at minimum state that you use the variable "val" with %pythonappend, and maybe also give this reference example since C# and Java both have an example for this use case and it's relevant to most (all?) memory managed language targets.

When a function returns a pointer/reference to a member, we don't want the owning object to get cleaned up while the managed code still holds the wrapper of the pointer. For C#, the following strategy is given in the docs:

Re: Pointer to member memory management in Python

Hi Jake

SWIG is an all volunteer project and we accept documentation patches to the html documentation too, so anyone can contribute. The python docs are in Doc/Manual/Python.html and this would make a nice example.

Although undocumented as far as I can tell, this causes the wrapped value to be stored in a local variable called "val", so the equivalent of the C# example becomes:

%pythonappend getWheel %{

val._bike = self

%}

I think it would be worth updating the docs to at minimum state that you use the variable "val" with %pythonappend, and maybe also give this reference example since C# and Java both have an example for this use case and it's relevant to most (all?) memory managed language targets.

When a function returns a pointer/reference to a member, we don't want the owning object to get cleaned up while the managed code still holds the wrapper of the pointer. For C#, the following strategy is given in the docs:

Re: Pointer to member memory management in Python

SWIG is an all volunteer project and we accept documentation patches to the html documentation too, so anyone can contribute. The python docs are in Doc/Manual/Python.html and this would make a nice example.

Although undocumented as far as I can tell, this causes the wrapped value to be stored in a local variable called "val", so the equivalent of the C# example becomes:

%pythonappend getWheel %{

val._bike = self

%}

I think it would be worth updating the docs to at minimum state that you use the variable "val" with %pythonappend, and maybe also give this reference example since C# and Java both have an example for this use case and it's relevant to most (all?) memory managed language targets.

When a function returns a pointer/reference to a member, we don't want the owning object to get cleaned up while the managed code still holds the wrapper of the pointer. For C#, the following strategy is given in the docs:

Re: Pointer to member memory management in Python

Hi Jake,

thanks for this.

I'm not entirely sure, but isn't this (i.e. the keeping of a reference to self) something that the "default" typemap for returned references from non-static member methods in classes should (or could) do? At least right now I cannot think of an example
where these don't return an interior reference (at least in our code-bases).

As another use of "val", I'm using it to cast to the derived type for a method that returns references of the base class, e.g.

SWIG is an all volunteer project and we accept documentation patches to the html documentation too, so anyone can contribute. The python docs are in Doc/Manual/Python.html and this would make a nice example.

Although undocumented as far as I can tell, this causes the wrapped value to be
stored in a local variable called "val", so the equivalent of the C# example becomes:

%pythonappend getWheel %{

val._bike = self

%}

I think it would be worth updating the docs to at minimum state that you use
the variable "val" with %pythonappend, and maybe also give this reference example
since C# and Java both have an example for this use case and it's relevant to
most (all?) memory managed language targets.

When a function returns a pointer/reference to a member, we don't want the owning object to
get cleaned up while the managed code still holds the wrapper of the pointer. For C#, the
following strategy is given in the docs: