0

I can't remember exactly but I have been getting an error like "object disposed exception" some time ago, while testing and closing my app. Do I need to dispose each object manually? Or what's the proper way of using controls / objects?

The error indicated this line in main form:

Private th As New SellerTimerHandler

I get this error hardly (I can't test it) so I wonder general idea to avoid taking up space in memory unnecessarily, not only when executing and closing but also after crashings.

onurcano
  • 395
  • 2
  • 18

2 Answers2

1

A System.ObjectDisposedException is thrown when you are trying to access an object that is already disposed. The exception has nothing to do with a class/an object not being disposed.

When your application closes you don't need to do anything with its objects/resources as Windows takes care of that for you. The disposing and garbage collecting of objects only needs to be performed during runtime in order to free up memory for the application to be able to continue to operate, and for it to not eat up all your RAM.

When a process is running the operating system has full knowledge over all the system resources that it is using; meaning that when the process actually closes, the OS simply frees all the memory used by it (this applies to when a process crashes too).

Visual Vincent
  • 18,045
  • 5
  • 28
  • 75
1

I would guess you have a timer or something else running which periodically uses objects in your class which have been disposed by the GC upon closing.

You should implement IDisposable in your class. If it's a form, it already does and you should look in the designer for Dispose(bool)

If you have timers running you should stop them there and null them. In fact any managed object in your class should be disposed here. That way you can control the order in which your objects get disposed

Protected Overridable Sub Dispose(disposing As Boolean)
    If Not Me.disposedValue Then
        If disposing Then
            ' TODO: dispose managed state (managed objects).
            ' i.e. stop all timers
        End If

        ' TODO: free unmanaged resources (unmanaged objects) and override Finalize() below.
        ' TODO: set large fields to null.
    End If
    Me.disposedValue = True
End Sub
djv
  • 15,168
  • 7
  • 48
  • 72
  • If he only sees to it that nothing accesses his variable, that will be enough. There is no need to dispose anything when your application is about to close. [**Read this**](http://stackoverflow.com/a/13078157/3740093) – Visual Vincent Jul 15 '16 at 21:47
  • Clarifying my above comment: It is not wrong to implement `IDisposable`, it may actually be a rather good thing to do. Though you don't need to dispose _every object and every control_ in the form. -- **But** since the problem is due to an `ObjectDisposedException`, that means that the class has already been disposed/garbage collected when something tries to access it. – Visual Vincent Jul 15 '16 at 21:54
  • 2
    Fair enough. But it doesn't hurt to have control over the order in which they are cleared. Stopping timers first ensures that timers won't be looking for already disposed resources. Letting the GC determine the order makes OP ask this question :) – djv Jul 15 '16 at 21:56
  • Of course the Timers (if any) must be stopped first (anything accessing his variable must be blocked upon shutdown). However regarding regular objects that does not interact automatically with his variable there's no need to dispose them on your own. -- Any timers, threads, etc. can simply be stopped/blocked in the `FormClosing` event, since it is fired before the application starts closing. Utilizing the event is also a way of controlling the order. (: – Visual Vincent Jul 15 '16 at 22:00
  • And during all the talk about disposing and garbage collecting, [**this question**](http://stackoverflow.com/questions/38405622/vb-net-dispose-garbage-collector-not-releasing-resources) pops up :). – Visual Vincent Jul 15 '16 at 22:07
  • What a coincidence! – djv Jul 16 '16 at 04:04