Concerning delete/delete[] at program exit

This is a discussion on Concerning delete/delete[] at program exit within the C++ Programming forums, part of the General Programming Boards category; Originally Posted by brewbuck So we should poison the operating system so it explodes if someone does something Elysia doesn't ...

  1. #46
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,173
    Quote Originally Posted by brewbuck View Post
    So we should poison the operating system so it explodes if someone does something Elysia doesn't like?
    My opinion. Not relevant to you.

    Enough "FYI" already. Where the hell have you developed this wellspring of experience anyway?
    Ha! Only the gods know!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  2. #47
    Registered User
    Join Date
    Apr 2006
    Posts
    2,012
    Quote Originally Posted by Elysia View Post
    It should not be safe. Or should not be possible, or something. Optimization or not, I do not like the idea of doing this at all, for all reasons mentioned above.
    Most programs are liable to crash, without cleanup. Therefore the ability of an OS to safely cleanup is a vital feature that can be relied upon. If someone has an OS that cannot properly handle an application crash, then I suggest they get a new one.

    And how many programs can safely shut down and does not have some work to do when shutting down? Saving settings, perhaps? It's far better and safer to just shut down the normal way and let all destructors and cleanup code run.
    You're missing my point. There times when such a feature is a vital requirement -- an application must be able to safely and completely recover from a sudden system shutdown. Given such an application, one may as well take advantage of the feature to speed up the normal quiting process.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  3. #48
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,173
    Quote Originally Posted by King Mir View Post
    Most programs are liable to crash, without cleanup. Therefore the ability of an OS to safely cleanup is a vital feature that can be relied upon. If someone has an OS that cannot properly handle an application crash, then I suggest they get a new one.
    Of course. Due to all the poor written applications out there, the OS just has to have a feature to clean up the mess of programs. Sure. I can get that.

    You're missing my point. There times when such a feature is a vital requirement -- an application must be able to safely and completely recover from a sudden system shutdown. Given such an application, one may as well take advantage of the feature to speed up the normal quiting process.
    This is a good thing but that does not give it the right to just exit or crash or whatever and force the OS to cleanup its mess.
    As has been mentioned before, shutdown should not take long time and if it does, then it's probably not memory-related but destructors that needs to be optimized.
    And even if it takes long, it's far safer to let everything clean up the normal way in a background thread.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  4. #49
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Elysia, although I agree with you on this for 99.99% of the programs out there - they should always clean up their own mess, I think the point of this discussion is that this "technique" is an optimization. So as any optimization, it should be used very carefully and only when the benefit outweighs the cost.

  5. #50
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,173
    Optimizations do not need to be applied when applications are terminating IMO, but whatever.
    And I cannot agree it is an optimization; it's a hack.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  6. #51
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Quote Originally Posted by brewbuck View Post
    The stability of the OS should not depend on the behavior of user applications. That's the entire point of having an operating system. It's not a patch, it's a fundamental step forward.
    I was going to argue this point. Then I realized it was just my opinion and it was irrelevant.

    Quote Originally Posted by Elysia View Post
    No, I disagree. Even if programs take time to close down, they can do so in the background.
    Even shutdown time is something that you could use to be doing something else.

    Quote Originally Posted by brewbuck View Post
    Calling the difference between DOS (a system without memory protection) and any modern OS a "patch" is freaking nuts. The only reason DOS itself didn't have memory protection is because the home computer hardware at the time didn't support it.
    I didn't mean it was patching the OS, but rather the application.

    Quote Originally Posted by brewbuck View Post
    and for that matter how DOES a user application "free the stack" anyway?
    Nothing suggested that you have to do that. And you didn't allocate it so why should you have to free it? And what is a stack anyways?

    Quote Originally Posted by brewbuck View Post
    Many modern operating systems won't even ALLOW you to give memory back to the OS once you've acquired it. Your program's data size can grow but not shrink.
    Really? Can you support this?

    Quote Originally Posted by King Mir View Post
    If one of the features of an application you are developing is the ability to safely suddenly terminate at any time, then it is safe to call exit() at any time to force such a termination. Since the feature is there, one may as well use it to quickly close the application.
    There's also atexit.
    It's fast and guaranteed to work.
    Code:
    void do_this()
    {
        for(;;);
        return;
    }
    
    int main()
    {
        atexit(do_this);
        exit();
    
        return 0;
    }
    Quote Originally Posted by cpjust View Post
    Elysia, I don't think calling it a 'patch' is really correct. Everything since the 386 (using the Intel line as an example) has had Protected Memory mode built into the CPU, which allows 32-bit OS's to allocate a completely separate virtual memory space to each application. Unlike the Win 3.1 days when a GPF could crash all 16-bit programs that were running, and IPF in 32-bit Windows would only crash the program that caused the IPF. Since Intel provided Windows with a tool to protect separate memory namespaces, they decided to use it. This isn't a patch, it's a new feature that was virtually impossible in earlier CPUs.
    I don't see what memory protection has to do with this.

    Quote Originally Posted by Elysia View Post
    And how many programs can safely shut down and does not have some work to do when shutting down?
    Are you talking about from a library perspective or an application perspective?
    Saving settings, perhaps?
    That's what fflush, fclose, and unbuffered output are for.

    Hey George2, that's how you use the multi-quote feature.

  7. #52
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,173
    Quote Originally Posted by robwhit View Post
    Are you talking about from a library perspective or an application perspective?
    From an application perspective, perhaps even library, all the same.

    That's what fflush, fclose, and unbuffered output are for.
    But then again, the code has to execute to do that, doesn't it? So that means you can't call exit. And if it's placed inside a destructor, you need to delete that object.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  8. #53
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by Elysia View Post
    But then again, the code has to execute to do that, doesn't it? So that means you can't call exit. And if it's placed inside a destructor, you need to delete that object.
    Then put it in atexit().

  9. #54
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Quote Originally Posted by cpjust View Post
    Then put it in atexit().
    That would probably be an awful idea since you have no idea where and why atexit was called.

    But Elysia, I think you are taking it all too religiously. I guess very few people would disagree that proper code would delete all resources, and noone would disagree that you absolutely have to delete some resources.

    But if you have something like a FLTK_Window which is guaranteed not to do anything (useful) in its destructor, heaven and earth won't collapse if you fail to delete it at program termination.

    In the referred thread someone also mentions that omitting deletes may be so as to not complicate simple examples with irrelevant details. Just like error handling for input is omitted in most example programs.

    But may-be you should try to enlighten them?
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  10. #55
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,173
    If I tried to enlighten them, they'd probably sneeze at me, saying it's not necessary.
    But I'd love to see their faces when something breaks and they have to go to great lengths to fix it simply because they're not deleting what they allocate.
    I'd also love to see their faces if the OS didn't clean up their mess. Though I doubt that will ever happen.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  11. #56
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,586
    Calling the difference between DOS (a system without memory protection) and any modern OS a "patch" is freaking nuts. The only reason DOS itself didn't have memory protection is because the home computer hardware at the time didn't support it.
    As has been said this ability has been in Intel processors for a very very long time. Microsoft just decided to start using it very late in the game. I believe it all started with the 386 but I do remember some blurbs from way back about the 286 having some type of special 'mode' that was almost like protected mode. However it was not used often because it was clunky and not very stable. Matsp would probably know more about this than I.

    To me not cleaning up memory at program exit because the OS will do it for you is both lazy and a hack. To say it is an optimization is quite a stretch. In my 23 years of working with computers I can remember about 5 times I complained about a program shutting down too slow. Usually they startup and run too slow. It's not because none of them were slow but it's probably due to perception. At shutdown I'm done with the application and don't care what it does or how fast it is. At startup or during runtime I do. So optimize the startup and the runtime I don't really care about shutdown.

    I just cannot vote for the side that says not cleaning up memory at exit is a valid practice. We all agree it's not a good practice in an embedded environment and I say it's also equally not good in a non-embedded environment.

    Yes the OS will clean up for you as a courtesy to the other programs to keep the system stable. It's not designed so that programmers can be lazy and thoughtless at shutdown and force or rely 100% on the OS to clean up. The end result here is not the issue. It's not the fact that it will work either. It's the fact that regardless of the outcome, it's not a good practice.

    Just my two cents.
    Last edited by VirtualAce; 01-07-2008 at 05:08 PM.

  12. #57
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    I expected that more people would reply to my post. Oh well. Guess I'll wrap up my train of thought.

    Well, I think it all comes down to whether the OS releasing memory is a documented feature of Windows or some API, so that it can be relied upon, not just some expectant realization.

    That's all I have to say on the subject.

    edit: except for this:
    That would probably be an awful idea since you have no idea where and why atexit was called.
    It's called by exit. well... the function passed to it is. That's what you meant, right?

    edit2: and that I don't think it should be done in C++ because there might be some sort of compiler-added overload in the destructors, so the only way you could do it cleanly in C++ was with malloc and with OS extensions, or with compiler and OS extensions.
    Last edited by robwhit; 01-09-2008 at 12:59 PM.

  13. #58
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,173
    I'm with Bubba on this and I doubt you'll find any API or documentation on that it does this. Or if you do, I don't think you'll find it says "you can just avoid cleaning up so Windows can do it for you."
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  14. #59
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by Elysia View Post
    I'm with Bubba on this and I doubt you'll find any API or documentation on that it does this. Or if you do, I don't think you'll find it says "you can just avoid cleaning up so Windows can do it for you."
    I think it's very bad practice too, but none the less, it should be safe when used VERY carefully on the right platform, and it should shutdown a lot faster. That being said -- don't do it.

Page 4 of 4 FirstFirst 1234
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. HELP with Program!
    By afnitti in forum C Programming
    Replies: 9
    Last Post: 04-15-2009, 08:06 PM
  2. exit a program at any point.
    By slightofhand in forum C Programming
    Replies: 5
    Last Post: 03-02-2008, 08:08 AM
  3. Program Terminating With Error On Exit
    By chriscolden in forum C Programming
    Replies: 19
    Last Post: 01-14-2006, 03:40 AM
  4. Program uses a lot of memory and doesnt exit properly
    By TJJ in forum Windows Programming
    Replies: 13
    Last Post: 04-28-2004, 03:13 AM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21