question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Calling overridden method with out parameter from C# to python results in 0xC0000005 (Access Violation)

See original GitHub issue

Environment

  • Pythonnet version: 2.5.2
  • Python version: 3.8.3
  • Operating System: Win10 64bit
  • .NET Runtime: 4.8

Details

  • Describe what you were trying to get done.

In C# I have defined an interface with a method with an out parameter. I implemented this interface in python, an now I want to call this method from C#. (using pythonnet)

What I see is that a method without out parameter works fine, but a method with an out parameter throws an access violation.

I know that out parameters are handled differently in pythonnet: you return a tuple with first the return value and the second tuple item is the out parameter.

My C# code

public interface IMyInterface
{
    string MyMethod_Out(string name, out int index);
    string MyMethod(string name);
}

public class MyServer
{
    public void CallMyMethod_Out(IMyInterface myInterface)
    {
        Console.WriteLine("C#.CallMyMethod_Out: before MyMethod_Out");
        int index = 1;
        myInterface.MyMethod_Out("myclient", out index);
        Console.WriteLine($"C#:CallMyMethod_Out: after MyMethod_Out: index:{index}");
    }
    
    public void CallMyMethod(IMyInterface myInterface)
    {
        Console.WriteLine("C#.CallMyMethod: before MyMethod");
        myInterface.MyMethod("myclient");
        Console.WriteLine($"C#:CallMyMethod: after MyMethod");
    }

    public void DoSomething()
    {
        Console.WriteLine("C#.DoSomething");
    }
}

My Python code:

import clr
import sys

sys.path.append('some_dir')
clr.AddReference("ClassLibrary1")

from ClassLibrary1 import IMyInterface, MyServer

class MyInterfaceImpl(IMyInterface):
    __namespace__ = "ClassLibrary1"

    def MyMethod_Out(self, name, index):
        print("Python.MyInterfaceImpl.MyMethod_Out")
        other_index = 101
        return ('MyName', other_index)

    def MyMethod(self, name):
        print("Python.MyInterfaceImpl.MyMethod")
        return 'MyName'

print('\nCall regular C# function')
my_server = MyServer()
my_server.DoSomething()

my_interface_impl = MyInterfaceImpl()

print('\nCall without out param')
my_server.CallMyMethod(my_interface_impl)

print('\nCall with out param')
my_server.CallMyMethod_Out(my_interface_impl) # Access Violation 0xC0000005
  • If there was a crash, please include the traceback here. Output:

Call regular C# function C#.DoSomething

Call without out param C#.CallMyMethod: before MyMethod Python.MyInterfaceImpl.MyMethod C#:CallMyMethod: after MyMethod

Call with out param C#.CallMyMethod_Out: before MyMethod_Out Process finished with exit code -1073741819 (0xC0000005)

Expected output

… Call with out param C#.CallMyMethod_Out: before MyMethod_Out Python.MyInterfaceImpl.MyMethod_Out C#:CallMyMethod_Out: after MyMethod_Out: index:101

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
lostmsucommented, Jan 6, 2022
1reaction
roy-van-de-korputcommented, Jul 6, 2021

I just retried with master (ea61b038c0f26854787638f6e29b5c2027894927) . Same error.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Calling overridden method with out parameter from C# to ...
What I see is that a method without out parameter works fine, but a method with an out parameter throws an access violation....
Read more >
What does an unhandled exception, 0xC0000005, mean in ...
0xC0000005 is a code for Access Violation error. It means that your program just tried to read, or write, in a section of...
Read more >
Access Violation C0000005
An Access Violation is a type of Exception caused when an application Reads, Writes or Executes an invalid Memory Address.
Read more >
Using GDAL in Python class results in access violation ...
I am getting Access Violation error when running (some) GDAL functions from a class method, while running a straight script works fine. Process...
Read more >
[Solved]-Calling virtual method in c++ gives access violation-C ...
C++ Inheritance : Calling virtual method when it has been overridden ... unmanaged C++ library (dll) from C# creates an access violation error...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found