I’m trying to read MP3 file from microSD card and to announce it by using the MP3 extension for FEX Domino.
Sometime it works but sometimes I get the following exception when reading the file:
A first chance exception of type ‘System.ObjectDisposedException’ occurred in Microsoft.SPOT.IO.dll
#### Exception System.ObjectDisposedException - CLR_E_OBJECT_DISPOSED (7) ####
#### Microsoft.SPOT.IO.NativeFileStream::GetLength [IP: 0000] ####
#### System.IO.FileStream::get_Length [IP: 001b] ####
Uncaught exception
Message: Exception was thrown: System.ObjectDisposedException
#### Exception System.ObjectDisposedException - CLR_E_OBJECT_DISPOSED (9) ####
#### Microsoft.SPOT.IO.NativeFileStream::GetLength [IP: 0000] ####
#### System.IO.FileStream::get_Length [IP: 001b] ####
Uncaught exception
Stack Trace: Microsoft.SPOT.IO.NativeFileStream::Read
System.IO.FileStream::Read
Here is the basic code:
using System;
using System.Text;
using System.Threading;
using System.IO;
using System.IO.Ports;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using Microsoft.SPOT.IO;
using GHIElectronics.NETMF.IO;
public static void Main()
{
FileStream file= null;
byte[] mp3data = new byte[2048];
string mp3SampleFile;
// microSD Card is inserted
// Create a new storage device
PersistentStorage sdPS = new PersistentStorage("SD");
// Mount the file system
sdPS.MountFileSystem();
if (VolumeInfo.GetVolumes()[0].IsFormatted)
{
string rootDirectory = VolumeInfo.GetVolumes()[0].RootDirectory;
string[] files = Directory.GetFiles(rootDirectory);
string[] folders = Directory.GetDirectories(rootDirectory);
mp3SampleFile = Directory.GetFiles(folders[0]);
}
else
{
Debug.Print("Storage is not formatted. Format on PC with FAT32/FAT16 first.");
}
EZ_Extensions.MP3.Initialize();
// The loudest volume
FEZ_Extensions.MP3.SetVolume(255, 255);
try
{
file = File.Open(mp3SampleFile , FileMode.Open, FileAccess.Read);
int size;
do
{
size = file.Read(mp3data, 0, 2048);
FEZ_Extensions.MP3.SendData(mp3data);
} while (size > 0);
}
catch (Exception ex)
{
Debug.Print("Message: " + ex.Message);
Debug.Print("Stack Trace: " + ex.StackTrace);
Debug.GC(true);
}
finally
{
if (file != null)
file.Close();
}
return 0;
}
}
You’r right, my mistake, I mismatched code of two functions.
I have Thread.sleep(Timeout.Infinite) at the end of my code, not return 0.
Any way, the exception is still happening.
The problem is not in GC, your code is disposing an object (or dereferencing an object) and then later you are trying to use an object that is no longer available.
Can you give more explanations?
The exception happened when calling to file.Read in the loop that read the mp3 file.
Where and what object is disposed during that process?
The GetLength call also failed, so it’s not going anywhere.
Here’s a question, how many times does your read-loop step around? IE do you read 2k of the file before failure? 4k?
You can do a few different things here to try to diagnose.
Write a simple program that does nothing related to the MP3 decoder, just handles the file. When that works, you know that adding in the MP3 decoding bits will work.
find a sample program that handles files that you can learn from the structure of their file handling.
debug.print lots of places, and use F11 to step through your code, to see where things are actually going wrong.
a combination of all of them!
But fundamentally, you aren’t handling the file stream properly and fixing that will let you move past that error…
I still don’t understand what object is disposed.
The problem is that when I debug step by step nothing happen and the same when I close the call to FEZ_Extensions.MP3.SendData();
Just when I run it without debugging and call the function, the Exception happened.
I close the file handle just after calling to FEZ_Extensions.MP3.SendData(), so, why its disposed?