Skip to content

Crash on Event invocation with ref parameter #1355

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
noambonnieclear opened this issue Jan 14, 2021 · 7 comments
Closed

Crash on Event invocation with ref parameter #1355

noambonnieclear opened this issue Jan 14, 2021 · 7 comments

Comments

@noambonnieclear
Copy link

noambonnieclear commented Jan 14, 2021

Environment

  • Pythonnet version: 2.5.1
  • Python version: 3.6.5
  • Operating System: Windows 10
  • .NET Runtime: 4.8.4250.0

Details

  • When I try to invoke a .NET event that takes a ref parameter, the python process crashes. I provided the code to reproduce the issue below.

Note: I cannot change the code to not use ref as the ref delegate is coming from a 3rd party library. I can wrap around it with a lightweight C# piece of code but would rather avoid it if possible.
Note: This may be related to the following issue: #965

Windows Event Viewer Error 1: Source - .NET Runtime

Application: python.exe
Framework Version: v4.0.30319
Description: The process was terminated due to an internal error in the .NET Runtime at IP 00007FF81CEA3D2E (00007FF81CEA0000) with exit code 80131506.

Windows Event Viewer Error 2: Source - Application Error

Faulting application name: python.exe, version: 3.6.5150.1013, time stamp: 0x5abbcb08
Faulting module name: clr.dll, version: 4.8.4250.0, time stamp: 0x5f2a059c
Exception code: 0xc0000005
Fault offset: 0x0000000000003d2e
Faulting process id: 0x4348
Faulting application start time: 0x01d6eaa4a034a863
Faulting application path: C:\Users\noam.bonnie\dev\hp-bluetooth-service\.tox\py36\Scripts\python.exe
Faulting module path: C:\Windows\Microsoft.NET\Framework64\v4.0.30319\clr.dll
Report Id: c2031029-3b2f-4e61-aa84-5e5502e904d2
Faulting package full name: 
Faulting package-relative application ID: 

The C# class with ref parameter for the delegate. Removing the ref resolves the problem

using System;

namespace TestEvent
{
    public delegate void MyEvent(ref string data); 

    public class MyTest
    {
        public event MyEvent myEvent;

        public virtual void triggerEvent()
        {
            Console.WriteLine("Invoking Event!");
            string myStr = "This is the event data";
            myEvent?.Invoke(ref myStr);
        }
    }
}    

Python program to invoke the event. Crashes.

import clr
import os
import sys


def handleEventData(data):
    print("=== event invoked. data: {} ===".format(data))


try:
    # load the library of the C# class above from a 'lib' local directory. 
    # not necessary if the library of the C# dll is in the path
    lib_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'lib')
    sys.path.append(lib_path)
    clr.AddReference("TestEvent")
    clr.AddReference("System")

    # Print the .NET version
    import System
    print(System.Runtime.InteropServices.RuntimeInformation.FrameworkDescription)

    # Create object, subscribe to event and trigger the event
    import TestEvent
    t = TestEvent.MyTest()
    t.myEvent += handleEventData
    t.triggerEvent()
except Exception as e:
    print(e)
@tminka
Copy link
Contributor

tminka commented Jan 21, 2021

Do you need handleEventData to modify data? Or do you want it to behave like a non-ref event handler?

@lostmsu
Copy link
Member

lostmsu commented Jan 21, 2021

Also see this comment: #1364 (comment) re: modification.

@noambonnieclear
Copy link
Author

Do you need handleEventData to modify data? Or do you want it to behave like a non-ref event handler?

I don't need to modify the data. I think the reason they designed it this way is because the data is a long string so they're trying to avoid copy.

@lostmsu
Copy link
Member

lostmsu commented Jan 21, 2021

Not related to the issue, but in .NET strings are pointers, so passing them does not make a copy of the characters, but only of the pointer...

@tminka
Copy link
Contributor

tminka commented Jan 28, 2021

Fixed by #1364

@noambonnieclear
Copy link
Author

Thank you so much for the quick turnaround time on this @tminka.

  • Reading through your PR and comments, I'm not sure I understand - will event handlers be receiving a tuple now?
  • Is there a way to subscribe to release notifications for the project? (so I know when this fix is released). Sorry if I'm exposing my github ignorance here :-)

@tminka
Copy link
Contributor

tminka commented Jan 28, 2021

  1. The event handler handleEventData that you wrote will work, as long as it returns the new value of data. Since you said you don't want to change data, just add return data to the end. See https://pythonnet.github.io/ for the full explanation, and test-event.py for more examples.
  2. At the top right of this page, click Watch and select Custom from the drop menu menu.

@lostmsu lostmsu closed this as completed Feb 21, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants