Skip to content

Rethink memory management (construction/destruction) #138

@sethrj

Description

@sethrj

Currently, instantiating and cleaning up classes is done in a very C++-like manner, with a constructor statement and a release statement. Assignment of proxy class objects generally acts like pointer association (=> in Fortran)

type(MyProxyClass) :: owned, referenced, also_referenced
owned = MyProxyClass(1, 2, 3) ! Construct a class
referenced = get_a_ref() ! Make 'referenced' point to some C++ data
referenced = owned ! Pointer assignment, doesn't change any data

which prohibits assignment of class data; so the following can't be used to change the contents of the vector:

type(VectorString), intent(in) :: vec
type(String) :: vec_ref
vec_ref = vec%get_ref(0) ! get a reference to a C++ string
vec_ref = String("new string") ! change the reference to point to a new string, doesn't change `vec`'s data

Is there a way to allocate, assign, point to, and deallocate with SWIG proxy types? Can we make it at least as safe as the current method, and as safe as native Fortran?

For example, something like

type(MyProxyClass), allocatable :: owned
type(MyProxyClass), pointer :: referenced, also_referenced
allocate(owned, source=MyProxyClass(1, 2, 3)) ! Construct a class from fortran
referenced => get_a_ref() ! Get a proxy class pointer to C++ data
also_referenced => owned ! Make a Fortran pointer also reference the origin class
owned = referenced ! Do assignment on underlying C++ objects
dellocate(owned) ! optional??? if FINAL behaves correctly
! deallocate(referenced)??
! deallocate(also_referenced)??

I don't think that native Fortran pointers are reference-counted, so I'm not sure how to safely obtain a C++ reference and make it appear as a Fortran pointer.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions