csharp: Refactor wrapper lifetime.
authorVitor Sousa <vitorsousa@expertisesolutions.com.br>
Fri, 31 May 2019 20:43:11 +0000 (17:43 -0300)
committerWonki Kim <wonki_.kim@samsung.com>
Mon, 3 Jun 2019 08:09:36 +0000 (17:09 +0900)
commit6040d2673c52a978af10ec7dafa2b9248964911a
treebbdd13a72945bdbb5bc40714fe83237fe55ed737
parentb02b64fba74477bedf4c52e5833a8ede137f712b
csharp: Refactor wrapper lifetime.

Summary:
This commit makes use of the `ownership,shared` and `ownership,unique`
events from Efl.Object in order to avoid the C# wrapper from being
collected while C code holds a reference to the object.

For example, creating a list of items in a for loop and attaching events to
them would fails without this commit, as the C# GC may collect the wrapper.

The basic idea is that we use a `WrapperSupervisor`, which is stored in
the Eo data storage, with a GCHandle allocated for the lifetime of the
underlying Eo object. This supervisor takes care of holding either a
weak C# reference (when in unique mode, allowing the wrapper to be GC'd)
or a hard C# reference (when in shared mode, making the wrapper
non-collectable while the Eo has extra references).

One limitation is that object graphs can leak if a shared object in the
graph - an Eo child for example - stores a hard reference to another
object in the graph as a C# field. In this example, this causes the
parent to always have a hard C# reference (from the child) as the child
is non-collectable due to the parent holding an Eo reference to it.

Depends on D8678

Test Plan: `ninja test` and `make test`

Reviewers: lauromoura, felipealmeida, woohyun, segfaultxavi

Reviewed By: lauromoura

Subscribers: cedric, #reviewers, #committers

Tags: #efl

Differential Revision: https://phab.enlightenment.org/D9014
16 files changed:
src/Makefile_Efl_Mono.am
src/bin/eolian_mono/eolian/mono/events.hh
src/bin/eolian_mono/eolian/mono/function_definition.hh
src/bin/eolian_mono/eolian/mono/klass.hh
src/bindings/mono/eina_mono/eina_container_common.cs
src/bindings/mono/eo_mono/EoWrapper.cs [new file with mode: 0644]
src/bindings/mono/eo_mono/WrapperSupervisor.cs [new file with mode: 0644]
src/bindings/mono/eo_mono/iwrapper.cs
src/bindings/mono/eo_mono/meson.build
src/bindings/mono/eo_mono/workaround.cs
src/lib/efl_mono/efl_custom_exports_mono.c
src/tests/efl_mono/Eo.cs
src/tests/efl_mono/Inheritance.cs
src/tests/efl_mono/TestUtils.cs
src/tests/efl_mono/dummy_part_holder.c
src/tests/efl_mono/dummy_part_holder.eo