Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #12890

Re: noob question - destructors - freeing memory...

From "MikeWhy" <boat042-nospam@yahoo.com>
Newsgroups comp.lang.c++
Subject Re: noob question - destructors - freeing memory...
Date 2011-12-30 21:22 -0600
Organization A noiseless patient Spider
Message-ID <jdlv5m$43a$1@dont-email.me> (permalink)
References <jdlpc1$cdi$1@dont-email.me> <jdlqo9$i9j$1@dont-email.me> <4efe7460$0$293$14726298@news.sunsite.dk>

Show all headers | View raw


someone wrote:
> On 12/31/2011 03:06 AM, MikeWhy wrote:
>> someone wrote:
>>> Hi
>>>
>>> See this (stolen from the internet):
>>>
>>> ----------------------------------------
>>> #include <iostream>
>>> using namespace std;
>>>
>>> class Base
>>> {
>>> public:
>>> Base(){ cout<<"Constructing Base"<<endl;}
>>>
>>> // this is a destructor:
>>> virtual ~Base(){ cout<<"Destroying Base"<<endl;}
>>> };
>>>
>>> class Derive: public Base
>>> {
>>> public:
>>> Derive(){ cout<<"Constructing Derive"<<endl;}
>>> ~Derive(){ cout<<"Destroying Derive"<<endl;}
>>> };
>>>
>>> int main()
>>> {
>>> Base *basePtr = new Derive();
>>> // delete basePtr; // <<<======== !! ! ! ! ! NB !!
>>> return 0;
>>> }
>>>
>>> ----------------------------------------
>>>
>>> See middle line of main():
>>>
>>> delete basePtr; // <<<======== !! ! ! ! ! NB !!
>>>
>>>
>>> If I enable (uncomment) middle line, I get:
>>> Constructing Base
>>> Constructing Derive
>>> Destroying Derive
>>> Destroying Base
>>>
>>>
>>> If I disable (comment out) middle line, I get:
>>> Constructing Base
>>> Constructing Derive
>>>
>>>
>>> ========
>>>
>>> This, I didn't quite expect, because I thought that at the end of
>>> the program, all destructors where guaranteed to be called...
>>
>> If pointers had destructors, what action should they take?
>
> Delete themselves? But not before they've destructed the objects
> they're pointing to! After they've destructed (free'd) the
> dereferenced objects, the pointers can free themselves...
>
> Why doesn't that happen in the example in my post and in my full code?
>
>>> In my case, I have a longer program and I allocate memory with one
>>> of my constructors, hence it is crucial to free the memory, but
>>> since the virtual destructor is never called, I never free the
>>> memory... In addition: In my full program (not shown here), I 
>>> instantiate my
>>> new object like:
>>>
>>> int main(...)
>>> {
>>> ...
>>> Derive d1();
>>> return 0; // or exit(1); if something went wrong somewhere
>>> }
>>>
>>> Is it required that I do, like in the example above:
>>>
>>> int main(...)
>>> {
>>> ...
>>> Base *myPtr = new Derive();
>>
>> foo(); // insert this line. See below.
>>> delete myPtr;
>>> return 0; // or exit(1); if something went wrong somewhere
>>> }
>>>
>>> ???????
>>
>> Yes.
>
> Ok, but I don't understand why... I found this post:
> http://www.tech-archive.net/Archive/VC/microsoft.public.vc.mfc/2007-05/msg01433.html
>
> from where I qoute: "Static objects are deleted (so the destructor is
> called) when global variables are deallocated, so when the program
> exits."
> I believe this code:
>
> int main(...)
> {
>   ...
>   Derive d1();
>   return 0; // or exit(1); if something went wrong somewhere
> }
>
> Has d1 to be a static object, hence when the program exits, the
> destructor of d1 should be called just before exitting... Why don't
> this happen?
>
>>> But what if my code does an exit(1) somewhere, which it could do at
>>> least a dozens of times, in different parts of the program?
>>
>> exit() is a function call. In your final example above, if foo()
>> called exit(1), will your destructor be called? Given what you saw
>> earlier, how does exit() trigger process termination?
>
> exit() is not a normal function call. Normally, code execution
> continues right after the function calls. But not with exit(). Code
> below exit() is not executed.
>
>> If instead of calling exit(), foo() throws an uncaught exception. How
>> does the program behave?
>
> I later found some posts on google, suggesting to throw/catch
> exceptions, however I'm not very good at using exceptions and fear
> that the code will be much more complicated.
>
>> Calling exit() is a very bad idea. Ditto for terminating threads,
>> only more so. A simple return will suffice. Find some other
>> mechanism to trigger process (and thread) termination.
>
> return() has exactly the same problem as exit: The code below return()
> in main is not executed, hence I believe that exit() is some kind of
> acronym for return() (from the main thread)...

return is a C/C++ keyword. exit() is a function call. It kills the process 
and does not return. Since exit() doesn't return, foo() doesn't return. 
Since foo() doesn't return, execution will not continue in main(). Your 
delete will not be called.

You're asking the right questions. You now need to understand the 
distinction.

There also was a reason to bring up exceptions. The exception would have 
behaved as you thought exit() might. In the contexts you describe -- calling 
exit(1) in multiple locations -- the semantics and behavior are as you 
expected from exit().


> Anyway, please explain why the static destructor is not called on
> exit. I don't understand why not...

The truth of the matter is that pointers do not have destructors. My 
intention was not to lead you astray, however. You'll visit this again in 
shared_ptr and auto_ptr classes.

Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

noob question - destructors - freeing memory... someone <newsboost@gmail.com> - 2011-12-31 02:43 +0100
  Re: noob question - destructors - freeing memory... "MikeWhy" <boat042-nospam@yahoo.com> - 2011-12-30 20:06 -0600
    Re: noob question - destructors - freeing memory... someone <newsboost@gmail.com> - 2011-12-31 03:33 +0100
      Re: noob question - destructors - freeing memory... "MikeWhy" <boat042-nospam@yahoo.com> - 2011-12-30 21:22 -0600
        Re: noob question - destructors - freeing memory... someone <newsboost@gmail.com> - 2011-12-31 04:57 +0100
          Re: noob question - destructors - freeing memory... "MikeWhy" <boat042-nospam@yahoo.com> - 2011-12-30 22:28 -0600
            Re: noob question - destructors - freeing memory... "MikeWhy" <boat042-nospam@yahoo.com> - 2011-12-30 22:43 -0600
            Re: noob question - destructors - freeing memory... someone <newsboost@gmail.com> - 2011-12-31 05:49 +0100
              Re: noob question - destructors - freeing memory... "MikeWhy" <boat042-nospam@yahoo.com> - 2011-12-30 23:56 -0600
                Re: noob question - destructors - freeing memory... someone <newsboost@gmail.com> - 2011-12-31 07:07 +0100
                Re: noob question - destructors - freeing memory... Nick Keighley <nick_keighley_nospam@hotmail.com> - 2011-12-31 01:50 -0800
                Re: noob question - destructors - freeing memory... someone <newsboost@gmail.com> - 2011-12-31 18:13 +0100
                Re: noob question - destructors - freeing memory... Nobody <nobody@nowhere.com> - 2012-01-01 08:37 +0000
                Re: noob question - destructors - freeing memory... LR <lruss@superlink.net> - 2012-01-01 10:54 -0500
                Re: noob question - destructors - freeing memory... someone <newsboost@gmail.com> - 2012-01-02 00:33 +0100
                Re: noob question - destructors - freeing memory... "MikeWhy" <boat042-nospam@yahoo.com> - 2012-01-01 19:20 -0600
                Re: noob question - destructors - freeing memory... someone <newsboost@gmail.com> - 2012-01-02 03:55 +0100
                Re: noob question - destructors - freeing memory... Stefan Ensslen <stefan@zollsoft.de> - 2012-01-02 04:32 -0800
                Re: noob question - destructors - freeing memory... someone <newsboost@gmail.com> - 2012-01-02 17:40 +0100
                Re: noob question - destructors - freeing memory... Dombo <dombo@disposable.invalid> - 2012-01-02 19:14 +0100
                Re: noob question - destructors - freeing memory... someone <newsboost@gmail.com> - 2012-01-02 21:30 +0100
                Re: noob question - destructors - freeing memory... jgk@panix.com (Joe keane) - 2012-01-07 23:23 +0000
                Re: noob question - destructors - freeing memory... "MikeWhy" <boat042-nospam@yahoo.com> - 2012-01-07 17:43 -0600
                Re: noob question - destructors - freeing memory... Garrett Hartshaw <ghartshaw@gmail.com> - 2012-01-01 22:54 -0500
                Re: noob question - destructors - freeing memory... someone <newsboost@gmail.com> - 2012-01-02 05:29 +0100
                Re: noob question - destructors - freeing memory... Nick Keighley <nick_keighley_nospam@hotmail.com> - 2012-01-01 13:38 -0800
                Re: noob question - destructors - freeing memory... someone <newsboost@gmail.com> - 2012-01-02 00:35 +0100
      Re: noob question - destructors - freeing memory... "Bo Persson" <bop@gmb.dk> - 2011-12-31 12:55 +0100
  Re: noob question - destructors - freeing memory... Garrett Hartshaw <ghartshaw@gmail.com> - 2011-12-30 22:50 -0500
    Re: noob question - destructors - freeing memory... someone <newsboost@gmail.com> - 2011-12-31 05:37 +0100
      Re: noob question - destructors - freeing memory... LR <lruss@superlink.net> - 2011-12-31 01:07 -0500
        Re: noob question - destructors - freeing memory... someone <newsboost@gmail.com> - 2011-12-31 08:54 +0100
          Re: noob question - destructors - freeing memory... Nick Keighley <nick_keighley_nospam@hotmail.com> - 2011-12-31 02:05 -0800
            Re: noob question - destructors - freeing memory... someone <newsboost@gmail.com> - 2011-12-31 18:22 +0100
              Re: noob question - destructors - freeing memory... Nick Keighley <nick_keighley_nospam@hotmail.com> - 2012-01-01 13:45 -0800
                Re: noob question - destructors - freeing memory... Jorgen Grahn <grahn+nntp@snipabacken.se> - 2012-01-02 12:17 +0000
                Re: noob question - destructors - freeing memory... someone <newsboost@gmail.com> - 2012-01-02 17:48 +0100
                Re: noob question - destructors - freeing memory... Jorgen Grahn <grahn+nntp@snipabacken.se> - 2012-01-02 20:02 +0000
              Re: noob question - destructors - freeing memory... Nick Keighley <nick_keighley_nospam@hotmail.com> - 2012-01-01 13:42 -0800
      Re: noob question - destructors - freeing memory... Nick Keighley <nick_keighley_nospam@hotmail.com> - 2011-12-31 01:52 -0800
        Re: noob question - destructors - freeing memory... someone <newsboost@gmail.com> - 2011-12-31 18:23 +0100
      Re: noob question - destructors - freeing memory... Garrett Hartshaw <ghartshaw@gmail.com> - 2011-12-31 12:35 -0500
        Re: noob question - destructors - freeing memory... someone <newsboost@gmail.com> - 2012-01-02 00:47 +0100
  Re: noob question - destructors - freeing memory... Juha Nieminen <nospam@thanks.invalid> - 2011-12-31 09:40 +0000
    Re: noob question - destructors - freeing memory... someone <newsboost@gmail.com> - 2011-12-31 18:26 +0100
      Re: noob question - destructors - freeing memory... Garrett Hartshaw <ghartshaw@gmail.com> - 2011-12-31 13:00 -0500
      Re: noob question - destructors - freeing memory... Nobody <nobody@nowhere.com> - 2012-01-01 08:57 +0000
        Re: noob question - destructors - freeing memory... someone <newsboost@gmail.com> - 2012-01-02 00:43 +0100
      Re: noob question - destructors - freeing memory... Juha Nieminen <nospam@thanks.invalid> - 2012-01-01 09:03 +0000
        Re: noob question - destructors - freeing memory... someone <newsboost@gmail.com> - 2012-01-02 00:49 +0100
          Re: noob question - destructors - freeing memory... LR <lruss@superlink.net> - 2012-01-01 21:11 -0500
            Re: noob question - destructors - freeing memory... POSSIBLE SOLUTION FOUND? someone <newsboost@gmail.com> - 2012-01-02 05:23 +0100
              Re: noob question - destructors - freeing memory... POSSIBLE SOLUTION
 FOUND? Garrett Hartshaw <ghartshaw@gmail.com> - 2012-01-02 00:14 -0500
                Re: noob question - destructors - freeing memory... POSSIBLE SOLUTION FOUND? someone <newsboost@gmail.com> - 2012-01-02 17:55 +0100
                Re: noob question - destructors - freeing memory... POSSIBLE SOLUTION FOUND? someone <newsboost@gmail.com> - 2012-01-02 22:17 +0100
                Re: noob question - destructors - freeing memory... POSSIBLE SOLUTION
 FOUND? Garrett Hartshaw <ghartshaw@gmail.com> - 2012-01-02 16:40 -0500
                Re: noob question - destructors - freeing memory... POSSIBLE SOLUTION FOUND? someone <newsboost@gmail.com> - 2012-01-03 00:48 +0100
              Re: noob question - destructors - freeing memory... POSSIBLE SOLUTION FOUND? LR <lruss@superlink.net> - 2012-01-02 14:47 -0500
                Re: noob question - destructors - freeing memory... POSSIBLE SOLUTION FOUND? someone <newsboost@gmail.com> - 2012-01-03 00:43 +0100
                Re: noob question - destructors - freeing memory... POSSIBLE SOLUTION FOUND? LR <lruss@superlink.net> - 2012-01-02 20:30 -0500
                Re: noob question - destructors - freeing memory... POSSIBLE SOLUTION FOUND? someone <newsboost@gmail.com> - 2012-01-03 19:08 +0100
                Re: noob question - destructors - freeing memory... POSSIBLE SOLUTION FOUND? LR <lruss@superlink.net> - 2012-01-03 17:46 -0500
                Re: noob question - destructors - freeing memory... POSSIBLE SOLUTION FOUND? someone <newsboost@gmail.com> - 2012-01-04 21:11 +0100
                Re: noob question - destructors - freeing memory... POSSIBLE SOLUTION FOUND? LR <lruss@superlink.net> - 2012-01-04 18:56 -0500
                Re: noob question - destructors - freeing memory... POSSIBLE SOLUTION
 FOUND? Garrett Hartshaw <ghartshaw@gmail.com> - 2012-01-03 19:49 -0500
                Re: noob question - destructors - freeing memory... POSSIBLE SOLUTION FOUND? LR <lruss@superlink.net> - 2012-01-03 21:51 -0500
                Re: noob question - destructors - freeing memory... POSSIBLE SOLUTION
 FOUND? Garrett Hartshaw <ghartshaw@gmail.com> - 2012-01-03 23:37 -0500
                Re: noob question - destructors - freeing memory... POSSIBLE SOLUTION FOUND? LR <lruss@superlink.net> - 2012-01-04 14:24 -0500
                Re: noob question - destructors - freeing memory... POSSIBLE SOLUTION FOUND? someone <newsboost@gmail.com> - 2012-01-04 21:20 +0100
                Re: noob question - destructors - freeing memory... POSSIBLE SOLUTION FOUND? LR <lruss@superlink.net> - 2012-01-04 18:51 -0500
                Re: noob question - destructors - freeing memory... POSSIBLE SOLUTION FOUND? someone <newsboost@gmail.com> - 2012-01-05 07:32 +0100
          Re: noob question - destructors - freeing memory... Garrett Hartshaw <ghartshaw@gmail.com> - 2012-01-01 23:02 -0500
            Re: noob question - destructors - freeing memory... someone <newsboost@gmail.com> - 2012-01-02 05:33 +0100
            Re: noob question - destructors - freeing memory... "io_x" <a@b.c.invalid> - 2012-01-02 11:05 +0100
          Re: noob question - destructors - freeing memory... Juha Nieminen <nospam@thanks.invalid> - 2012-01-02 06:22 +0000
            Re: noob question - destructors - freeing memory... someone <newsboost@gmail.com> - 2012-01-02 18:03 +0100
  Re: noob question - destructors - freeing memory... "Bo Persson" <bop@gmb.dk> - 2011-12-31 12:51 +0100
    Re: noob question - destructors - freeing memory... someone <newsboost@gmail.com> - 2011-12-31 18:18 +0100

csiph-web