Disposing of Objects

Hi all,

My project involves writing a bootloader program to assign peripherals (buttons, light sensors, motor controllers, and LEDs) to ports of the FEZ Cerberus board.

My bootloader program listens to the serially connected bluetooth device and allows the interpretation of a user string.

Based on the user string, I “assign” a peripheral to a port determined by the string. That is, my bootloader code utilizes the new construct to assign a button, for example, to a port. This works well on the first run.

However, my code goes back into a “waiting” state for a second user string, and when the user sends a string that attaches to the same port, it fails.

Stepping through the code reveals that it fails on the second call to the new construct for that peripheral.

How do I properly deallocate the port from usage by a particular peripheral?

I have so far tried setting the existing peripheral to null before running a second time, but this does not work. Is this because the GC does not run to dispose of the object?

Thank you very much.


First try object.Dispose() and then set to null.

Next try setting object to null and then Debug.GC(true);

Hi Mike,

Thanks for the reply.

The object I think you are referring to is each of the peripherals I would assume? For example the button, however, these do not inherit from System.IDisposable and therefore don’t have a dispose. I did however try setting the object to null, however that did not seem to clear it up. Or am I misunderstanding this step?

I will try setting the object to null and Debug.GC(true); and see if that works in the meantime.

Thanks again.


With Gadgeteer, you might have to release the socket also. Look at the Gadgeteer source and see if it gives you a clue.

Hi Mike,

Thanks for the heads up on the socket. I found the following:
on the request for a socket which will allow dynamic loading of the sockets at runtime. However, it does unfortunately point that it has not been implemented to the current version of the Gadgeteer 2.42.600 release.

I would think that even if I null the object and get the GC to run, it may not release the socket, since it has been programmed to stay fixed during runtime. Do you think trying to let the GC run would still be a feasible fix?

Thanks much.


@ naveej - Sounds like GC will fix the problem. You could modify the Gadgeteer code?