Main Site Documentation

System.ObjectDisposedException


#1

I tried the the example code in section 31.2 Object Allocation (free eBook version 1.02), with my FEZ Domino.

using System.Threading;
using System;
using Microsoft.SPOT.Hardware;
using Microsoft.SPOT;

namespace MFConsoleApplication1
{
    public class Program
    {
        static SPI _spi = new SPI(new SPI.Configuration(
            Cpu.Pin.GPIO_NONE, false, 0, 0, false,
            true, 1000, SPI.SPI_module.SPI1));
        static byte[] buffer = new byte[2];

        static void WriteRegister(byte register_num, byte value)
        {
            buffer[0] = register_num;
            buffer[1] = value;
            _spi.Write(buffer);
            _spi.Dispose();
        }
        public static void Main()
        {
        ...

I got an Unhandled exception error message:

 ####  Exception System.ObjectDisposedException - 0x00000000 (1)  ####
 ####  ...

After some trials & errors, I commented out the line says: 
       _spi.Dispose();

in the " WriteRegister method like this:
The program works!

        static void WriteRegister(byte register_num, byte value)
        {
            buffer[0] = register_num;
            buffer[1] = value;
            _spi.Write(buffer);

           // _spi.Dispose();
 }

Is this because NETMF automatically dispose the buffer? Anyone?

sam


#2

If you tried using the _spi object after it was disposed you will get an error.

Some tips to get more responses:

  1. Use the code formatting tags to make your code more readable.
  2. Include the whole exception output text. This contains a lot of valuable info that can help others spot where the problem is.

#3

Looks like the spi object is being disposed twice. That would explain the exception.

What you did is not quite the same as the example in the ebook.


#4

Like Jeff said, the easier you make it for other to understand your code and problem the more answers you will get back. I personally hate reading code that is not code tagged. Very much pain to read :slight_smile:

When you dispose an object that means you no longer need to use it. So, why are you disposing the objects?


#5

Sorry guys,
I did try to use the quote tool, code tool in my message.
But It did not work for me, since the new tool bar above the reply message. :frowning:
I could not use the quote, code, hyperlink, bold, italic, etc…
I remember it used to work for me then, before the web page was changed to the new design.
I could not even put the smiley face into my message box anymore.
:frowning: :frowning: <— it happened like this!!!

Is this something to do with the viewer?
I’m using Internet Explorer 8 on the XP Pro system.

sorry again,

sam


#6

Oh, yeah. The formatting tools are not working in IE8 right now (forgot that until I was just trying to post some code). Hopefully Josh will get that fixed soon ::slight_smile:


#7

Sure he will, he is always working on the website, so he’ll get it fixed :slight_smile:


#8

Jeff,

Thanks for clearing me out of thisguilt! hehehe…
(You bad bad IE8…)
Should I switch to something else like FireFox, etc Any suggestion??

sam


#9

I use IE8 for everything and only use FF when I need to use FireBug or test my own website.


#10

Asking such things on the internet usually starts huge discussions. :smiley:

Anyway. I personally recommend to use google chrome. Other users will recommend firefox.
It’s a personal thing and I would suggest to try both of them :slight_smile:


#11

Thanks everyone for all the help and tips and suggestions.
I think I got the ideal of how to use SPI Write and Dispose along the way.

And again thanks Jeff. It seems like the thing got fixed now.
It work!

using System.Threading;
using System;
using Microsoft.SPOT.Hardware;
using Microsoft.SPOT;
...

Hooray! :stuck_out_tongue:

sam