From: Sean Middleditch Date: 2002-02-25T05:32:06+09:00 Subject: Re: Object/Memory Management On Sun, 2002-02-24 at 15:05, Jim Weirich wrote: > >>>>> "SeanM" == Sean Middleditch writes: > > SeanM> [...] The stack does *not* in any way guarantee that those > SeanM> objects are cleaned up. You can leave functions without > SeanM> cleaning up (destroying) objects on the stack. Errors or > SeanM> exceptions can occur during stack cleanup that you can't > SeanM> catch or deal with. The order of object destruction is (in > SeanM> many compilers) not guaranteed. [...] > > Actually, if we are talking about stack based C++ objects here, this > is incorrect. The order of destruction of stack based objects is > always reverse order of creation. They are guaranteed to be > destructed upon leaving the scope of the function, even in the > presence of exceptions (setjump/longjump does cause problems). Jumping was the one issue I was referring. The order of destructors, while supposedly a standard, doesn't work on all C++ compilers. Thankfully, the major players (VC++, GCC, Borland C++, Watcom C++) all work right in this regard. Granted, I've never used a compiler that *didn't* work on, but I've heard storied from people trying to port C++ code. > > Since C++ doesn't have a finally statement, or easy to use closures, > the stack based destructor trick is used pervasively. Many C++ > programmers try to take this technique with them when trying other > languages. Unfortunately, I've only seen it work effectively in C++ The stack based destructor still falls apart too easily, especially when errors occur in the destructor. > > -- > -- Jim Weirich jweirich@one.net http://w3.one.net/~jweirich > --------------------------------------------------------------------- > "Beware of bugs in the above code; I have only proved it correct, > not tried it." -- Donald Knuth (in a memo to Peter van Emde Boas)