5

This question has been bugging me for a while: I've read in MSDN's DirectX article the following:

The destructor (of the application) should release any (Direct2D) interfaces stored...

DemoApp::~DemoApp()
{
    SafeRelease(&m_pDirect2dFactory);
    SafeRelease(&m_pRenderTarget);
    SafeRelease(&m_pLightSlateGrayBrush);
    SafeRelease(&m_pCornflowerBlueBrush);
}

Now, if all of the application's data is getting released/deallocated at the termination (source) why would I go through the trouble to make a function in-order-to/and release them individually? it makes no sense!

I keep seeing this more and more over the time, and it's obviously really bugging me. The MSDN article above is the first time I've encountered this, so it made sense to mention it of all other cases.

Well, since so far I didn't actually ask my questions, here they are:

  • Do I need to release something before termination? (do explain why please)
  • Why did the author in MSDN haven chosen to do that?
  • Does the answer differ from native & managed code? I.E. Do I need to make sure everything's disposed at the end of the program while I'm writing a C# program? (I don't know about Java but if disposal exists there I'm sure other members would appreciate an answer for that too).

Thank you!

Community
  • 1
  • 1
MasterMastic
  • 20,711
  • 12
  • 68
  • 90
  • 6
    Well, _memory_ is the only _one type of resources_. While memory will be released by operating system (because it is smart enough), some other resources can be possibly lost. – Lol4t0 Nov 16 '12 at 18:48
  • Is this question about C++ or C#? They're going to have _very_ different answers! – Mooing Duck Nov 16 '12 at 18:56
  • @MooingDuck I've actually asked (last question) what is the answer for both of these cases. – MasterMastic Nov 16 '12 at 19:00

2 Answers2

3

You don't need to worry about managed content when your application is terminating. When the entire process's memory is torn down all of that goes with it.

What matters is unmanaged resources.

If you have a lock on a file and the managed wrapper for the file handler is taken down when the application closes without you ever releasing the lock, you've now thrown away the only key that would allow access to the file.

If you have an internal buffer (say for logging errors) you may want to flush it before the application terminates. Not doing so would potentially mean the fatal error that caused the application to end isn't logged. That could be...bad.

If you have network connections open you'll want to close them. If you don't then the OS likely won't do it for you (at least not for a while; eventually it might notice the inactivity) and that's rather rude to whoever's on the other end. They may be continuing to listen for a response, or continuing to send you information, not knowing that you're not there anymore.

Servy
  • 202,030
  • 26
  • 332
  • 449
3

Now, if all of the application's data is getting released/deallocated at the termination (source) why would I go through the trouble to make a function in-order-to/and release them individually?

A number of reasons. One immediate reason is because not all resources are memory. Only memory gets reclaimed at process termination. If some of your resources are things like shared mutexes or file handles, not releasing those resources could mess up other programs or subsequent runs of your program.

I think there's a more important, more fundamental reason though. Not cleaning up after yourself is just lazy, sloppy programming. If you are lazy and sloppy in cleanup at termination, are you lazy and sloppy at other times? If your tendancy is to be lazy and sloppy and only override that tendancy in specific areas where you're cognizant of potential problems, then your tendancy is to be lazy and sloppy. What if there are potential problems you're not cognizant of? How can you rely on your overall philosophy of lazy, sloppy programming to write correct, robust programs?

Don't be that guy. Clean up after yourself.

John Dibling
  • 99,718
  • 31
  • 186
  • 324
  • 2
    To the point of being lazy, I disagree. Either tearing down the app handles it, and you don't need to, or it doesn't, and you do. I know I wouldn't spend time going through a condemed building scrubbing the floors the day before it's scheduled for demolition. – Servy Nov 16 '12 at 19:09
  • Having a program leave things for the OS to clean up isn't necessarily lazy programming, but a programmer should be aware of what assumptions are being made, and ensure that *some particular specification guarantees that such assumptions will in fact be correct*. Even when frameworks or operating systems specify that certain things should be explicitly cleaned up before they are abadoned, they will often nonetheless attempt to clean them up if they're abandoned without cleanup; the fact that such automatic cleanup "seems to work" doesn't mean it will be reliable. – supercat Nov 17 '12 at 19:47