#include <iostream>
#include <stdexcept>
struct Eulogy
{
~Eulogy() { std::cout << "Rest in peace?\n"; }
};
int main()
{
try
{
Eulogy e;
throw new std::runtime_error("error");
}
catch( ... )
{
std::cout << "...\n";
}
} I expect to see "Rest in peace?" followed by "..." but I only get "...". I compiled with exception handling and destructor cleanup on. (bcc32 -w -RT -x -xd -Od) MSVC does give me the expected result. Is this a compiler bug? Judging from past experience and the open bug reports I see at Embarcadero, I am not suprised. Thanks for reading, I know many of you probably stopped after the word 'Borland' ;)
🔒 Locked
Compiler bug...?
Advertisement
Original Post
Before I go submitting a bug report at Borland/Codegear/Embarcadero, I want to be sure I'm not crazy.. The following C++ code gives me unexpected results in C++Builder 2007
The only reason for the compiler not to call "e.~Eulogy()" would be if construction of e would fail, otherwise, upon unwinding, the dtor() must be called. This is what RAII is for, so probably you really have found a bug.
Just two questions:
0) Throwing around pointers is MFC style, and very error prone and outside of MFC no common practice. See this item. See, you have a leak in your code.
1) Why oh why that Borland ****?
Just two questions:
0) Throwing around pointers is MFC style, and very error prone and outside of MFC no common practice. See this item. See, you have a leak in your code.
1) Why oh why that Borland ****?
Yes I know it is leaking the exception, it is not my code... I am just the guy who has to debug it. I always create the exception 'normally' and catch by const-ref.
The reason why I am digging into this is because I was finally convincing my co-workers of the benefits of using RAII (they are still in the C-with-classes phase..) until one of them claimed "it did not always work". I said that was impossible, so imagine my surprise now..
As for 1), are you asking why I use Borland or why I badmouth it? The reason I have a low opinion of it is already explained by this post :) And this is not the first one of these quirks I find.. The reason I use it is because I have to. Our CTO makes bad calls.
The reason why I am digging into this is because I was finally convincing my co-workers of the benefits of using RAII (they are still in the C-with-classes phase..) until one of them claimed "it did not always work". I said that was impossible, so imagine my surprise now..
As for 1), are you asking why I use Borland or why I badmouth it? The reason I have a low opinion of it is already explained by this post :) And this is not the first one of these quirks I find.. The reason I use it is because I have to. Our CTO makes bad calls.
Quote:So why do you expect to see "Rest in peace" at all? When do you expect the destructor to be called?
Original post by Icon
Yes I know it is leaking the exception, it is not my code... I am just the guy who has to debug it. I always create the exception 'normally' and catch by const-ref.
The reason why I am digging into this is because I was finally convincing my co-workers of the benefits of using RAII (they are still in the C-with-classes phase..) until one of them claimed "it did not always work". I said that was impossible, so imagine my surprise now..
As for 1), are you asking why I use Borland or why I badmouth it? The reason I have a low opinion of it is already explained by this post :) And this is not the first one of these quirks I find.. The reason I use it is because I have to. Our CTO makes bad calls.
@Evil Steve: The exception object is leaking. But I still expect Eulogy to be destroyed as part of stack unwinding due to exiting the scope by a (leaky) exception. Am I expecting too much?
@visitor: I thought of that as well, but the object is really created. I added a trace in the constructor but I did not paste that code for brevity. When I said C-with-classes I actually meant this: http://www.nicollet.net/2009/04/cpp-confusion/ (section Modern C++, 3rd paragraph) :)
@visitor: I thought of that as well, but the object is really created. I added a trace in the constructor but I did not paste that code for brevity. When I said C-with-classes I actually meant this: http://www.nicollet.net/2009/04/cpp-confusion/ (section Modern C++, 3rd paragraph) :)
Quote:The exception isn't allocated on the stack, so it won't be destroyed. You'd need to delete it instead. It's the same idea as returning an object allocated with new, it won't be destroyed when the function returns.
Original post by Icon
@Evil Steve: The exception object is leaking. But I still expect Eulogy to be destroyed as part of stack unwinding due to exiting the scope by a (leaky) exception. Am I expecting too much?
Icon: I meant not why you badmouth it, but why you and your fellow programmers use that **** of a compiler :D
The compiler is allowed to inline the call, indeed. And it is allowed to do so much more, but he/she is not allowed to change program behaviour (look at the corpus of ~Eulogy(), which dumps text to cout (probably to stdout)).
edit: Btw, when I write a small benchmark, I often make the codepath dependent on user input or other unforseeable values (e.g. reading clock() in an effort to optimize away some optimization (e.g. "cin >> b; for (int i=0; i<b; ++i)" mostly saves you from full unrolling).
Quote:
Original post by visitor
May-be the construction of the object is optimized away (is the compiler allowed to?)?
The compiler is allowed to inline the call, indeed. And it is allowed to do so much more, but he/she is not allowed to change program behaviour (look at the corpus of ~Eulogy(), which dumps text to cout (probably to stdout)).
edit: Btw, when I write a small benchmark, I often make the codepath dependent on user input or other unforseeable values (e.g. reading clock() in an effort to optimize away some optimization (e.g. "cin >> b; for (int i=0; i<b; ++i)" mostly saves you from full unrolling).
Quote:He's not talking about the exception, though, the destructor he's expecting to run is the one for the Eulogy class. It should run...
Original post by Evil Steve
The exception isn't allocated on the stack, so it won't be destroyed. You'd need to delete it instead. It's the same idea as returning an object allocated with new, it won't be destroyed when the function returns.
Quote:D'oh, I need to learn to read, sorry [smile]
Original post by Codeka Quote:He's not talking about the exception, though, the destructor he's expecting to run is the one for the Eulogy class. It should run...
Original post by Evil Steve
The exception isn't allocated on the stack, so it won't be destroyed. You'd need to delete it instead. It's the same idea as returning an object allocated with new, it won't be destroyed when the function returns.
Certainly looks like a compiler bug. Can you step through the code? Better yet, take a look at the compiled assembly.
Certainly looks like a compiler bug. Can you step through the code? Better yet, take a look at the compiled assembly.
Look, I have found another thread with the same problem.
Whoa, when I look at more bugs of bcc in the internet, everything but not using it is an epic sin, ..., no, an Epic Sin of DEATH. There were even bugs where braces are eaten up upon code parsing .... horrible. And afair, a comment like "workaround for borland c++ builder" can be found in many boost files.
[Edited by - phresnel on April 14, 2009 7:12:18 AM]
Whoa, when I look at more bugs of bcc in the internet, everything but not using it is an epic sin, ..., no, an Epic Sin of DEATH. There were even bugs where braces are eaten up upon code parsing .... horrible. And afair, a comment like "workaround for borland c++ builder" can be found in many boost files.
[Edited by - phresnel on April 14, 2009 7:12:18 AM]
Yes, you should check out Embarcadero Quality Central forum, a misnomer if I ever saw one.. For boost < 1.36 there is actually a separate project, bcbboost, to make it compile with Cbuilder. And even then half of it does not work.
Thanks for all the replies. I did look at the assembly but could find out much. I guess I'll put my hope in CBuilder 2009.. And as a workaround start looking for another job.
Thanks for all the replies. I did look at the assembly but could find out much. I guess I'll put my hope in CBuilder 2009.. And as a workaround start looking for another job.
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement