Skip to content

Commit

Permalink
Keep reference to generic server function delegates so they don't get…
Browse files Browse the repository at this point in the history
… GCed and crash the application
  • Loading branch information
campersau committed Jan 20, 2022
1 parent 4c3fee9 commit 65dfaa8
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions src/SapNwRfc/SapServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,24 @@ public static void InstallGenericServerFunctionHandler(Func<string, SapAttribute
InstallGenericServerFunctionHandler(new RfcInterop(), getFunctionMetadata, action);
}

// We keep a reference to the current server function delegates so they don't get GCed
// Otherwise the application crashes as the delegate passed into native does no longer exists in managed
#pragma warning disable SA1306 // Field names should begin with lower-case letter
private static RfcInterop.RfcServerFunction CurrentServerFunction;
private static RfcInterop.RfcFunctionDescriptionCallback CurrentFunctionDescriptionCallback;
#pragma warning restore SA1306 // Field names should begin with lower-case letter

private static void InstallGenericServerFunctionHandler(RfcInterop interop, Func<string, SapAttributes, ISapFunctionMetadata> getFunctionMetadata, Action<ISapServerConnection, ISapServerFunction> action)
{
CurrentServerFunction = (IntPtr connectionHandle, IntPtr functionHandle, out RfcErrorInfo errorInfo)
=> HandleGenericFunction(interop, action, connectionHandle, functionHandle, out errorInfo);

CurrentFunctionDescriptionCallback = (string functionName, RfcAttributes attributes, ref IntPtr funcDescHandle)
=> HandleGenericMetadata(functionName, attributes, out funcDescHandle, getFunctionMetadata);

RfcResultCode resultCode = interop.InstallGenericServerFunction(
serverFunction: (IntPtr connectionHandle, IntPtr functionHandle, out RfcErrorInfo errorInfo)
=> HandleGenericFunction(interop, action, connectionHandle, functionHandle, out errorInfo),
funcDescPointer: (string functionName, RfcAttributes attributes, ref IntPtr funcDescHandle)
=> HandleGenericMetadata(functionName, attributes, out funcDescHandle, getFunctionMetadata),
serverFunction: CurrentServerFunction,
funcDescPointer: CurrentFunctionDescriptionCallback,
out RfcErrorInfo installFunctionErrorInfo);

resultCode.ThrowOnError(installFunctionErrorInfo);
Expand Down

0 comments on commit 65dfaa8

Please sign in to comment.