2

I've seen that multiple times. Last time i saw it when somebody created an excel application object. He ended his application like that:

myExcelObj = Nothing
Application.Exit()

I'm sure making the reference point to nothing won't close the excel application running in the backround. Also saw that alot with non visible objects like the following:

Public Class myClass
   Var1 as Integer = 0
   Var2 as SQLConnection
   Var3 as Whatever

   Public Sub New()
      Var1 = Maths.Rnd(0,1)
      Var2 = SomeStaticClass.GetSQLConnection()
      Var3 = New Whatever(Var1)
   End Sub
End Sub

(...)

// Somewhere in the Code (...)
Private Sub EndItAll(sender as Object, e as EventArgs)
    Me.My_myClassObject = Nothing
    Application.Exit()
End Sub

Does that make any sense? Won't closing the application itself free every used memory anyways? When does it make sense to do so?

Luke
  • 751
  • 2
  • 7
  • 32
  • 4
    It does not make sense. Lots of .NET programmers don't really understand how the garbage collector works and acquired bad habits, especially so in Office interop code. Some background in [this Q+A](https://stackoverflow.com/questions/17130382/understanding-garbage-collection-in-net). – Hans Passant May 29 '17 at 10:52
  • 2
    As always a great answer by you. That'll add another one to the "Luke gives Hans Passant Reputation"-Counter. Thank you. – Luke May 29 '17 at 10:59
  • I'd check to see if there are multiple "hidden" instances of Excel still running in the background (Task Manager) when this is done. Excel interop objects tend to keep Excel running if not properly cleaned up. That might not be the case here, but it looks like he's cleaning up the objects VBA-style. He likely got that habit from using VBA. There might not be other problems but I'd check for those still-running Excel apps just in case. – Scott Hannen May 30 '17 at 01:20

2 Answers2

1

This might answer your question: https://blogs.msdn.microsoft.com/ericlippert/2004/04/28/when-are-you-required-to-set-objects-to-nothing/

But that ignoring that little link right there, no it kinda makes no sense, at least in this day and age, like he says in the post, perhaps in an older version that was required.

MrSanchez
  • 317
  • 4
  • 14
  • 1
    Thanks for your Link. So as I expected it is not required anymore. – Luke May 29 '17 at 10:59
  • You're welcome ;) from an experience perspective I haven't programmed in VB.net for a couple of years but I never had to do it before exiting the application, it could prevent future bugs, but I don't think there will ever be a bug that will prevent the memory dumping unless there is a bug that the application exits but the process keeps running just because it's locked into a file. But you're welcome ;) – MrSanchez May 29 '17 at 11:03
1

Before the application ends? Pointless.

Otherwise it is good practice, IMHO.

Mike
  • 419
  • 4
  • 11