MIDIout.Finalize() MIDIin.Finalize() problem

Mar 15, 2012 at 7:24 AM
Edited Mar 15, 2012 at 7:47 AM


Hello Mark!
Im working with midi in and midi out.
I see if for error, you use same port like in and out, error MIDout.Finalize() or  MIDIin.Finalize() happen.
I asume this is caused by midi loop or something like that or some loop?
Some times is midiin ereor other times midiout, always if select the same port as midi in and midi out.

The problem is Naudio show exeption window, and no matter how many time i click on abort or ignore, all is freezed and i have to kill the aplication using task manager, but some tiems cant kill and need a hard reboot, other times need wait lot of time.
How can prevent this happen?
I can add logic on the software for prevent select the same port, but i use my software for send midi to other appz, and if that appz, have midi in and midi out in same port (i use midi yoke virtual midi wire) the same happen.

Also due is handled by naudio i can see in what portion of code this happen for try add and cathc this exeption at my way.

How can make my software catch that exeption, instead leave naudio doit?

This some kind of loop, and if this happen i need stop this happen and show some message then close the software probably, but prevent the complete freeze.

This is a problem cna be fixed from your side?
I itest with midi monitor software and not see midi signal loop feedback, so is something wrong inside the library probably.

Hoppe can helpme, is really ugly error and big problem.
Best regards!

Coordinator
Mar 15, 2012 at 12:09 PM

you need to make sure you call Dispose() on your MidiIn and MidiOut to avoid that assertion. It is just a Debug.Assert, warning you that you didn't call Dispose

Mar 15, 2012 at 4:32 PM
Edited Mar 15, 2012 at 4:33 PM

Mark run dispose when and where?

I have this error inmediatly after loas appz, without send or recive midi.

MIDIin.Finalize() happen when my appz use same port in in and out

 MIDout.Finalize()  when midi in and out is the same on appz, where i send midi form my appz trough midiyoke virtual midi wire.


I repeat i nto send midi so where call dispose?

I think you need take a look on this please.

The only place where i call dispose is when i close my appz, and run midiin dispose and midi out dispose.
This error happen inmediatly after load appz without send or use midi.

Coordinator
Mar 15, 2012 at 4:44 PM

how many times do you call new MidiIn or new MidiOut?

Mar 15, 2012 at 4:54 PM
Edited Mar 16, 2012 at 4:24 AM

 

 

One time onload:


Gmidi.midiOut = new MidiOut(Gmidi.VMidiDevice);
this.Closed += (o, s) => Gmidi.midiOut.Dispose();
Gmidi.midiIn = new MidiIn(Gmidi.IMidiDevice);
this.Closed += (o, s) => Gmidi.midiIn.Dispose();
Gmidi.midiIn.Start();



Then the error happen, and i not make nothing or send midi or anything.
Also, using midi monitor, no any signal midi is sended.
I suspect this is sme kind of bug or problem requeire some workaround on the library.
Like always, thankyou so much for send a fatser repply : )
Hoppe this can be fixed please.

Coordinator
Mar 16, 2012 at 6:58 AM

the finalizer is called by the .NET runtime, not by NAudio. If you are seeing it, it means that there is a way your midiOut or midiIn is losing its reference. Is there anywhere in your code that sets Gmidi.midiOut = null, or Gmidi.midiIn = null?

Also, have a look at the NAudioDemo which does not have this problem.

Mar 16, 2012 at 5:36 PM

Ultra srange no any call to Gmidi.midiOut = null, or Gmidi.midiIn = null?

I cant compile the demo for see, tonz of errors.
Anyway i take a look and if cnat solve i make simple example and upload so you can see what im doing. : )
I think be the best way to not run this in a tonz of answers and questions and save your time : )
I back soon with some news.

Thankyou for your support!

Apr 12, 2012 at 5:01 PM
Edited Apr 13, 2012 at 1:51 AM

Check this:

Instead run onload window I run this inside the constructor:
Gmidi.midiOut = new MidiOut(Gmidi.VMidiDevice);
this.Closed += (o, s) => Gmidi.midiOut.Dispose();
Gmidi.midiIn = new MidiIn(Gmidi.IMidiDevice);
this.Closed += (o, s) => Gmidi.midiIn.Dispose();
Gmidi.midiIn.Start();




Line report the error is Gmidi.midiOut = new MidiOut(Gmidi.VMidiDevice);

 

From my appz, I send midi to other software, if in software where i send MIDI, midi out and midi in is configured to same midi device port, this happen.



If I run this on onload
Gmidi.midiOut = new MidiOut(Gmidi.VMidiDevice);
this.Closed += (o, s) => Gmidi.midiOut.Dispose();
Gmidi.midiIn = new MidiIn(Gmidi.IMidiDevice);
this.Closed += (o, s) => Gmidi.midiIn.Dispose();
Gmidi.midiIn.Start();


Complete visual studio crash.



NAudio.MmException was unhandled
  HResult=-2146233088
  Message=UnspecifiedError calling midiOutOpen
  Source=NAudio
  StackTrace:
       at NAudio.MmException.Try(MmResult result, String function)
       at NAudio.Midi.MidiOut..ctor(Int32 deviceNo)
       at window2.Window2..ctor() in window2.xaml.cs:line 161
       at MainWindow.timer_surface1(Object sender, EventArgs args) in MainWindow.xaml.cs:line 829
       at System.Windows.Threading.DispatcherTimer.FireTick(Object unused)
       at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
       at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler)
       at System.Windows.Threading.DispatcherOperation.InvokeImpl()
       at System.Windows.Threading.DispatcherOperation.InvokeInSecurityContext(Object state)
       at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Windows.Threading.DispatcherOperation.Invoke()
       at System.Windows.Threading.Dispatcher.ProcessQueue()
       at System.Windows.Threading.Dispatcher.WndProcHook(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
       at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
       at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)
       at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
       at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler)
       at System.Windows.Threading.DispatcherOperation.InvokeImpl()
       at System.Windows.Threading.DispatcherOperation.InvokeInSecurityContext(Object state)
       at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Windows.Threading.DispatcherOperation.Invoke()
       at System.Windows.Threading.Dispatcher.InvokeImpl(DispatcherOperation operation, CancellationToken cancellationToken, TimeSpan timeout)
       at System.Windows.Threading.Dispatcher.LegacyInvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Int32 numArgs)
       at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)
       at MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg)
       at System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame)
       at System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame frame)
       at System.Windows.Threading.Dispatcher.Run()
       at System.Windows.Application.RunDispatcher(Object ignore)
       at System.Windows.Application.RunInternal(Window window)
       at System.Windows.Application.Run(Window window)
       at System.Windows.Application.Run()
       at App.Main() in App.g.cs:line 0
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException:

 

 

If I setup the same midi device in out on my software the error is generated by:

Gmidi.midiIn = new MidiIn(Gmidi.IMidiDevice);

An this is the error:

NAudio.MmException was unhandled
  HResult=-2146233088
  Message=UnspecifiedError calling midiInOpen
  Source=NAudio
  StackTrace:
       at NAudio.MmException.Try(MmResult result, String function)
       at NAudio.Midi.MidiIn..ctor(Int32 deviceNo)
       at window2.Window2..ctor() in windows2.xaml.cs:line 162
       at MainWindow.timer_surface1(Object sender, EventArgs args) in MainWindow.xaml.cs:line 829
       at System.Windows.Threading.DispatcherTimer.FireTick(Object unused)
       at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
       at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler)
       at System.Windows.Threading.DispatcherOperation.InvokeImpl()
       at System.Windows.Threading.DispatcherOperation.InvokeInSecurityContext(Object state)
       at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Windows.Threading.DispatcherOperation.Invoke()
       at System.Windows.Threading.Dispatcher.ProcessQueue()
       at System.Windows.Threading.Dispatcher.WndProcHook(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
       at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
       at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)
       at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
       at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler)
       at System.Windows.Threading.DispatcherOperation.InvokeImpl()
       at System.Windows.Threading.DispatcherOperation.InvokeInSecurityContext(Object state)
       at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Windows.Threading.DispatcherOperation.Invoke()
       at System.Windows.Threading.Dispatcher.InvokeImpl(DispatcherOperation operation, CancellationToken cancellationToken, TimeSpan timeout)
       at System.Windows.Threading.Dispatcher.LegacyInvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Int32 numArgs)
       at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)
       at MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg)
       at System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame)
       at System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame frame)
       at System.Windows.Threading.Dispatcher.Run()
       at System.Windows.Application.RunDispatcher(Object ignore)
       at System.Windows.Application.RunInternal(Window window)
       at System.Windows.Application.Run(Window window)
       at System.Windows.Application.Run()
       at App.Main() in App.g.cs:line 0
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException:

 

One comment, my software use 2 windows, one is secreen for setup midi device, the other is the software screen and one class where I have inside all things I need access form that 2 windows.
The windows are separate project.
On main Mianwindow I have a combobox for  select midi devices.

The structure of my projets is:
One class where I have inside all things need be accessed by othee windows, example:
 

 

NAudio.Midi;     

 

public static MidiIn midiIn;
public static MidiOut midiOut;
public static int VMidiDevice;
public static int IMidiDevice;

Other class for handle swtch form one windows to other.
 

 

The mainwindow where I have insidecombobox selector for select midi device. i call of course using NAudio.Midi;  and have referenced the library.



window2  is the screen where have all features of software I call i call of course using NAudio.Midi;  and have referenced the library.

 

InitializeComponent();


Gmidi.midiOut = new MidiOut(Gmidi.VMidiDevice);
this.Closed += (o, s) => Gmidi.midiOut.Dispose();
Gmidi.midiIn = new MidiIn(Gmidi.IMidiDevice);
this.Closed += (o, s) => Gmidi.midiIn.Dispose();
Gmidi.midiIn.Start();


Here is where error happen, but only if in my software I set same midi device for in and out, also same happen when  in software where I send midi midi in and midi out is configured to same device (same midi port)

 

 

public class Gmidi

{

Apr 13, 2012 at 2:50 AM
Edited Apr 14, 2012 at 5:21 PM

edited.

 

Apr 14, 2012 at 5:11 PM
Edited Apr 14, 2012 at 5:41 PM




try

{
Gmidi.midiOut = new MidiOut(Gmidi.VMidiDevice);
this.Closed += (o, s) => Gmidi.midiOut.Dispose();
Gmidi.midiIn = new MidiIn(Gmidi.IMidiDevice);
this.Closed += (o, s) => Gmidi.midiIn.Dispose();
Gmidi.midiIn.Start();

}

catch

{

MessageBox.Show("here message about midi errorbla bla bla." );
Debug.Listeners.Clear();
Application.Current.Shutdown();

}

This is a workaround, but no idea why this happen.

Coordinator
Apr 16, 2012 at 1:28 PM

before calling new MidiOut or new MidiIn, you must first Dispose of any old one.

e.g

 

if (Gmidi.midiOut != null)

    Gmidi.midiOut.Dispose();