Page MenuHomePhabricator

C#: Review custom marshalling behavior
Closed, ResolvedPublic

Description

Currently EFL# is using the same ICustomMarshaler-based classes for both @in and @out parameters, and for return values. This may cause problems as the methods called on the marshaler will differ based on the direction of the call. The table below summarizes the ICustomMarshaler behavior

Parameter directionCall directionPreparing callCleanup CallNotes
@inC# calling CMarshalManagedToNativeCleanupNativeData
@inC calling C#MarshalNativeToManagedCleanupManagedData
@out/returrnC# calling CMarshalNativeToManagedCleanupNativeData
@out/returrnC calling C#MarshalManagedToNativeCleanupManagedDataSpecial care needed when returning non @moved data to avoid leaks.

The diagrams below were generated with https://sequencediagram.org/

C# called from C

Source:

# To be used with https://sequencediagram.org/

title C# delegate being called from C

C->Marshaler:Start calling function

activate Marshaler
Marshaler->Marshaler: MarshalNativeToManaged for @in

Marshaler->Csharp method: User Method

activate Csharp method

Csharp method->Marshaler: Return value
deactivate Csharp method

Marshaler->Marshaler: MarshalManagedToNative for @out/ret

Marshaler->Marshaler: CleanupManagedData for @out/ret


Marshaler->Marshaler: CleanupManagedData for @in


C<--Marshaler:Return the values

deactivate Marshaler

C# calling C functions

# To be used with https://sequencediagram.org/

title C function called from C#

Csharp->Marshaler:Start calling function

activate Marshaler
Marshaler->Marshaler: MarshalManagedToNative for @in

Marshaler->C: Actual C function call

activate C

C->Marshaler: Return value
deactivate C

Marshaler->Marshaler: MarshalNativeToManaged for @out/return


Marshaler->Marshaler: CleanupManagedData for @out/ret
Marshaler->Marshaler: CleanupManagedData for @in

Csharp<--Marshaler:Return the values

deactivate Marshaler