EMX: Errors while removing USB Memory Stick

While removing a USB key from the USB host an exception is launched that stop my code:

[quote] #### Exception System.ArgumentOutOfRangeException - CLR_E_OUT_OF_RANGE (4) ####
#### Message:
#### System.Collections.ArrayList::get_Item [IP: 0000] ####
#### System.IO.FileSystemManager::ForceRemoveNameSpace [IP: 0026] ####
#### Microsoft.SPOT.IO.RemovableMedia::MessageHandler [IP: 005c] ####[/quote]

Here’s the code for the USBH class:

using System;
using System.IO;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.IO;
using GHIElectronics.NETMF.IO;
using GHIElectronics.NETMF.USBHost;

namespace USU
{
    public class USBH
    {
        // Hold a static reference in case the GC kicks in and disposes it 
        // automatically, note that we only support one in this example!
        private static PersistentStorage USBps;
        public bool is_cle_present = false;

        public USBH()
        {
            // Subscribe to USB events
            USBHostController.DeviceConnectedEvent += USB_connect_device;
            USBHostController.DeviceDisconnectedEvent += USB_disconnect_device;
        }

        public void USB_connect_device(USBH_Device device)
        {
            if(device.TYPE == USBH_DeviceType.MassStorage)
            {
                Debug.Print("USB Mass Storage detected...");
                USBps = new PersistentStorage(device);
                USBps.MountFileSystem();
                is_cle_present = true;
            }
        }

        public void USB_disconnect_device(USBH_Device device)
        {
            is_cle_present = false;
            Debug.Print("USB MASS Storage is ejected.");
            USBps.UnmountFileSystem();
        }
    }
}

Does anyone know how to solve that?

Ok I have solved that. In fact it seems that when the media is removed, the CLR calls the Removable.Eject event even if the apllication that not subscribbe to it, therefore this launch an exception.

Removing the

 USBHostController.DeviceDisconnectedEvent and adding 

and adding

RemovableMedia.Eject += RemovableMedia_Eject;

solves that issue.

here’s the method that handle media ejection

 void RemovableMedia_Eject(object sender, MediaEventArgs e)
        {
is_cle_present = false;
            Debug.Print("Storage \"" + e.Volume.RootDirectory + "\" is ejected.");
        }
    }

:

In fact i believed it was ok but just retrying:

[quote] #### Exception System.ArgumentOutOfRangeException - CLR_E_OUT_OF_RANGE (9) ####
#### Message:
#### System.Collections.ArrayList::get_Item [IP: 0000] ####
#### System.IO.FileSystemManager::ForceRemoveNameSpace [IP: 0026] ####
#### Microsoft.SPOT.IO.RemovableMedia::MessageHandler [IP: 005c] ####
Une exception de première chance de type ‘System.ArgumentOutOfRangeException’ s’est produite dans mscorlib.dll
Une exception non gérée du type ‘System.ArgumentOutOfRangeException’ s’est produite dans mscorlib.dll[/quote]

Will look into it

The strange thing is that it happens only one time, if I go on execution of the application, inserting/removing does not launch any exception.

Starting to isolate a possible bug, I discovered that in my application, if the key is connected at startup, exception is thrown when removing but if the key is inserted after the application starts, there is no pb.

Bug solved.

In my code, I was creating a new object Filestream in a method to retrieve xml data. But the filestream was never disposed. After disposing it manually at the end of the function, removing the memory stick do not launch the exception.