8000 Crash when calling back through method with `ref ValueType` parameter · Issue #965 · pythonnet/pythonnet · GitHub
[go: up one dir, main page]

Skip to content
Crash when calling back through method with ref ValueType parameter #965
Closed
@zooba

Description

@zooba

Hi. In response to this query I had one of my colleagues debug an issue, and he found that PythonNet does not correctly generate code when calling into a function with a "ref ValueType" parameter. In his words:

The problem is here in the PythonNet code:

        for (var i = 0; i < parameters.Length; ++i)
        {
            il.Emit(OpCodes.Ldloc_0);
            il.Emit(OpCodes.Ldc_I4, i);
            il.Emit(OpCodes.Ldarg, i + 1);
            if (parameterTypes[i].IsValueType)
            {
                il.Emit(OpCodes.Box, parameterTypes[i]);
            }
            il.Emit(OpCodes.Stelem, typeof(object));
        }

The code is trying to store all of the incoming arguments in an array of type Object[]. The cases are:

  1. If the incoming argument is of reference type, the reference can simply be stored in the array.
  2. If the incoming argument is of struct type, the argument needs to be boxed into an object, which can then be stored in the array.
  3. If the incoming argument is a “byref” (i.e., “ref” keyword in C#), more complicated handling is needed, including:
    o On the way in, the byref needs to read (via ldobj if the underlying type is a struct, or ldind.ref otherwise) to load the actual argument data that needs to flow through.
    o On the way out, the byref needs to written (via stobj or stind.ref) to make sure any updates applied by the callee are reflected back to the caller.

The code above handles #1 and #2, but doesn’t handle #3.


Hopefully that's enough for someone to create or contribute a fix.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0