Skip to content
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

Unity: Editor crash when (dis-)connecting input devices #318

Open
stekra opened this issue Jan 21, 2025 · 5 comments
Open

Unity: Editor crash when (dis-)connecting input devices #318

stekra opened this issue Jan 21, 2025 · 5 comments
Labels
question Just question about the library unity Issue is related to Unity game engine

Comments

@stekra
Copy link

stekra commented Jan 21, 2025

Hi, thank you so much for the awesome library.

I'm trying out a minimal note event handling script in Unity.
I made sure to incorporate the advice from: https://melanchall.github.io/drywetmidi/articles/devices/Common-problems.html
It works as expected during play.

Issue:
When a Midi Device is disconnected or reconnected the Unity Editor crashes. This seems to only happen after reloading Scripts/Domain at least twice.

Code:

public class DryWetMidiMinimalExample : MonoBehaviour {
    InputDevice input;

    void OnEnable() {
        input = InputDevice.GetByName("MIDI DEVICE NAME");
        input.EventReceived += OnEventReceived;
        input.StartEventsListening();
    }

    void OnEventReceived(object sender, MidiEventReceivedEventArgs e) {
        var midiDevice = (MidiDevice)sender;
        Debug.Log($"Received MIDI event {e.Event} from {midiDevice.Name}");

        if (e.Event is NoteOnEvent noteOn) {
            Debug.Log($"Note {noteOn.NoteNumber} pressed with velocity {noteOn.Velocity}");
        }
    }

    void OnDisable() {
        if (input != null) {
            input.StopEventsListening();
            input.EventReceived -= OnEventReceived;
            input.Dispose();
        }
    }
}

Platform: macOS M3
Unity Version: 2022.3
Library Version: 7.2.0

I can't tell whether I'm doing something wrong. I can help investigate further. No stresso tho.

@melanchall melanchall added the unity Issue is related to Unity game engine label Jan 22, 2025
@melanchall melanchall moved this from To do to In progress in DryWetMIDI Jan 22, 2025
@melanchall
Copy link
Owner

Hi,

Thanks for kind words about the library!

  1. Can you please say what error Unity shows when crash occurred? Does it show some message? As far as I know macOS suggests to look into a crash dump in such cases. Please send me the dump if you can get it.
  2. Also can you please say what OnEnable and OnDisable methods do (I'm not an expert in Unity)?

@melanchall
Copy link
Owner

@stekra Any news?

@melanchall melanchall added the question Just question about the library label Feb 3, 2025
@stekra
Copy link
Author

stekra commented Feb 3, 2025

Hi,
Thank you for following up on this.

  1. Unity closes without a message & its Crash Reporter appears. The editor log file contains the following relevant stacktrace:
Scanning for USB devices : 1.172ms

=================================================================
	Native Crash Reporting
=================================================================
Got a segv while executing native code. This usually indicates
a fatal error in the mono runtime or one of the native libraries 
used by your application.
=================================================================

=================================================================
	Native stacktrace:
=================================================================
	0x329546fd0 - /Applications/Unity/Hub/Editor/2022.3.37f1/Unity.app/Contents/Frameworks/MonoBleedingEdge/MonoEmbedRuntime/osx/libmonobdwgc-2.0.dylib : mono_dump_native_crash_info
	0x329509334 - /Applications/Unity/Hub/Editor/2022.3.37f1/Unity.app/Contents/Frameworks/MonoBleedingEdge/MonoEmbedRuntime/osx/libmonobdwgc-2.0.dylib : mono_handle_native_crash
	0x329491a14 - /Applications/Unity/Hub/Editor/2022.3.37f1/Unity.app/Contents/Frameworks/MonoBleedingEdge/MonoEmbedRuntime/osx/libmonobdwgc-2.0.dylib : mono_sigsegv_signal_handler_debug
	0x190f38184 - /usr/lib/system/libsystem_platform.dylib : _sigtramp
	0x16c66606c - /Users/stefan/Desktop/proj/Assets/Melanchall/DryWetMIDI/Melanchall_DryWetMidi_Native64.dylib : HandleSource
	0x16c666374 - /Users/stefan/Desktop/proj/Assets/Melanchall/DryWetMIDI/Melanchall_DryWetMidi_Native64.dylib : HandleNotification
	0x16c6663dc - /Users/stefan/Desktop/proj/Assets/Melanchall/DryWetMIDI/Melanchall_DryWetMidi_Native64.dylib : NotifyProc
	0x1ac8023fc - /System/Library/Frameworks/CoreMIDI.framework/Versions/A/CoreMIDI : _ZN11MIDIProcess6NotifyEPKvm
	0x1ac838ecc - /System/Library/Frameworks/CoreMIDI.framework/Versions/A/CoreMIDI : _XNotify
	0x1ac803198 - /System/Library/Frameworks/CoreMIDI.framework/Versions/A/CoreMIDI : mshMIGPerform
	0x190fea2c8 - /System/Library/Frameworks/CoreFoundation.framework/Versions/A/CoreFoundation : __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__
	0x190fea1e8 - /System/Library/Frameworks/CoreFoundation.framework/Versions/A/CoreFoundation : __CFRunLoopDoSource1
	0x190fe8b48 - /System/Library/Frameworks/CoreFoundation.framework/Versions/A/CoreFoundation : __CFRunLoopRun
	0x190fe7bc4 - /System/Library/Frameworks/CoreFoundation.framework/Versions/A/CoreFoundation : CFRunLoopRunSpecific
	0x191062e50 - /System/Library/Frameworks/CoreFoundation.framework/Versions/A/CoreFoundation : CFRunLoopRun
	0x16c666458 - /Users/stefan/Desktop/proj/Assets/Melanchall/DryWetMIDI/Melanchall_DryWetMidi_Native64.dylib : ThreadProc
	0x190f032e4 - /usr/lib/system/libsystem_pthread.dylib : _pthread_start
	0x190efe0fc - /usr/lib/system/libsystem_pthread.dylib : thread_start

Let me know if I can provide further data.

  1. Unity's OnEnable() runs when a script (MonoBehaviour) is initialized or re-enabled, OnDisable() runs when it's disabled or the GameObject it is attached to is destroyed. OnEnable() is typically used to set up event listeners and OnDisable() to unsubscribe and clean up resources. These methods can run multiple times per script per session (e.g. entering & exiting Play Mode).

@melanchall
Copy link
Owner

Hi @stekra,

Thanks for the info. Unfortunately it's not clear what's going wrong :-( Yes, some code executed from my native library but then it goes into _pthread_start system function. Need to think about it. I'll let you know if I find something.

@stekra
Copy link
Author

stekra commented Feb 5, 2025

Hi,
Thank you, from my end it's not urgent at all, so no worries. Was just curious if I'm using it wrong or if it's some quirk in its interaction with Unity.
Let me know if I can assist in any way.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Just question about the library unity Issue is related to Unity game engine
Projects
Status: In progress
Development

No branches or pull requests

2 participants