398,007 Members | 2,688 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 398,007 IT Pros & Developers. It's quick & easy.

exception handling problem in Borland C++ Builder 6 (BCB 5.6)

Serge Skorokhodov (216716244)
P: n/a
Hi,

I encounter a strange behavior of BCB 5.6. The example:

#include <stdexcept>
#include <memory>

using namespace std;

class A
{
public:
A() { cout << "A constructed.\n"; }
~A() { cout << "A destructed.\n"; }
};

void main()
{
try
{
auto_ptr<A> apA(new A);
throw logic_error("Too bad!");
}
catch (some_except& e)
{
cout << "Exception cought: " << e.what() << "\n";
}
}

The output:

A constructed.
Exception cought: Too bad!

i.e neither ~apA() nor ~A() are called. -xd option (desctuctor
cleanup enabled) does not work both in IDE and command prompt.

Does anybody knows how to force the standard behavior with
borland compiler?

--
Thanks in advance.
Jul 23 '05 #1
Share this Question
Share on Google+
11 Replies


wittempj@hotmail.com
P: n/a
I do not know the answer to your question, but I suspect the posted
code is not compiling - you missed a header and some_excpet is unknown.
When changed like this:
-#include <stdexcept>
-#include <memory>
-#include <iostream>
-
-using namespace std;
-
-class A
-{
-public:
- A() { cout << "A constructed.\n"; }
- ~A() { cout << "A destructed.\n"; }
-
-};

-int main()
-{
- try
- {
- auto_ptr<A> apA(new A);
- throw logic_error("Too bad!");
- }
- catch (exception& e)
- {
- cout << "Exception cought: " << e.what() << "\n";
- }

- return 0;
-}

and compiled with g++ the resulting executable yields the expected
resault

martin@ubuntu:~$ g++ test.cpp -o hello
martin@ubuntu:~$ ./hello
A constructed.
A destructed.
Exception cought: Too bad!
martin@ubuntu:~$

Jul 23 '05 #2

Serge Skorokhodov (216716244)
P: n/a
wittempj@hotmail.com wrote:
I do not know the answer to your question, but I suspect the
posted code is not compiling - you missed a header and
some_excpet is unknown. When changed like this:
<skip>

Sure you're correct, I just typed the sample in a hurry:) Thanks
for the corrections.
and compiled with g++ the resulting executable yields the
expected resault

martin@ubuntu:~$ g++ test.cpp -o hello
martin@ubuntu:~$ ./hello
A constructed.
A destructed.
Exception cought: Too bad!
martin@ubuntu:~$


It works fine with MSVC.NET 2003 also. It's really too bad that
we cannot use RAII with BCC 5.6:(

Still, I hope somebody knows workaround.

Jul 23 '05 #3

Old Wolf
P: n/a
> I encounter a strange behavior of BCB 5.6. The example:

#include <stdexcept>
#include <memory>

using namespace std;

class A
{
public:
A() { cout << "A constructed.\n"; }
~A() { cout << "A destructed.\n"; }
};

void main()
{
try
{
auto_ptr<A> apA(new A);
throw logic_error("Too bad!");
}
catch (some_except& e)
{
cout << "Exception cought: " << e.what() << "\n";
}
}

The output:

A constructed.
Exception cought: Too bad!

i.e neither ~apA() nor ~A() are called.


This obviously isn't your exact code, as it doesn't compile.
With some minor changes, it compiles and runs correctly for
me in BCB5. ("A destructed" displays before "Exception cought...").

Post some code you are actually using, and check your
compiler options. You might also have better results
posting in one of the Borland-specific newsgroups
(look up borland.public.cppbuilder.* on newsgroups.borland.com).

Jul 23 '05 #4

Serge Skorokhodov (216716244)
P: n/a
Old Wolf wrote:
I encounter a strange behavior of BCB 5.6. The example:

#include <stdexcept>
#include <memory>

using namespace std;

class A
{
public:
A() { cout << "A constructed.\n"; }
~A() { cout << "A destructed.\n"; }
};

void main()
{
try
{
auto_ptr<A> apA(new A);
throw logic_error("Too bad!");
}
catch (some_except& e)
{
cout << "Exception cought: " << e.what() << "\n";
}
}

The output:

A constructed.
Exception cought: Too bad!

i.e neither ~apA() nor ~A() are called.

This obviously isn't your exact code, as it doesn't compile.
With some minor changes, it compiles and runs correctly for
me in BCB5. ("A destructed" displays before "Exception cought...").

Post some code you are actually using, and check your
compiler options. You might also have better results
posting in one of the Borland-specific newsgroups
(look up borland.public.cppbuilder.* on newsgroups.borland.com).

Thanks for the reply. You're right, I've mistyped the example but
the idea is quite clear. The problem is that compiler settings
does not turn destructor cleanup on:(

Jul 23 '05 #5

Moskvichev Nikolay
P: n/a
Serge Skorokhodov (216716244) wrote:
It works fine with MSVC.NET 2003 also. It's really too bad that we
cannot use RAII with BCC 5.6:(


//snippets.cpp:

#include <stdexcept>
#include <memory>
#include <iostream>

using namespace std;

class A
{
public:
A() { cout << "A constructed.\n"; }
~A() { cout << "A destructed.\n"; }

};

int main()
{
try
{
auto_ptr<A> apA(new A);
throw logic_error("Too bad!");
}
catch (exception& e)
{
cout << "Exception cought: " << e.what() << "\n";
}

return 0;
}
///////////////////////////////////////////////////////////////////
bcc32 snippets.cpp:

Borland C++ 5.6.4 for Win32 Copyright (c) 1993, 2002 Borland
snippets.cpp:
Turbo Incremental Link 5.64 Copyright (c) 1997-2002 Borland
///////////////////////////////////////////////////////////////////

snippets.exe:
A constructed.
A destructed.
Exception cought: Too bad!

///////////////////////////////////////////////////////////////////
Jul 23 '05 #6

Pelle Beckman
P: n/a
Serge Skorokhodov (216716244) skrev:
Hi,

I encounter a strange behavior of BCB 5.6. The example:

#include <stdexcept>
#include <memory>

using namespace std;

class A
{
public:
A() { cout << "A constructed.\n"; }
~A() { cout << "A destructed.\n"; }
};

void main()
{
try
{
auto_ptr<A> apA(new A);
throw logic_error("Too bad!");
}
catch (some_except& e)
{
cout << "Exception cought: " << e.what() << "\n";
}
}

The output:

A constructed.
Exception cought: Too bad!

i.e neither ~apA() nor ~A() are called. -xd option (desctuctor cleanup
enabled) does not work both in IDE and command prompt.

Does anybody knows how to force the standard behavior with borland
compiler?

This is actually more of a question:
Won't this code always throw an exception? and
(in later posts) Serge says the output is:
martin@ubuntu:~$ ./hello
A constructed.
A destructed.
Exception cought: Too bad!
martin@ubuntu:~$
Shouldn't this be (I really ought to compile this myself and see).
A constructed.
Exception caught.
A destructed


-- Pelle
Jul 23 '05 #7

Serge Skorokhodov (216716244)
P: n/a
Moskvichev Nikolay wrote:

New information.

Today all examples (including a new one that is a VCL
application) start to work. The real application I've encountered
the problem in does not:( Neither IDE options nor direct editing
of option source nor pragma option -RT -x -xd at all possible
locations does not work:(
Serge Skorokhodov (216716244) wrote:
It works fine with MSVC.NET 2003 also. It's really too bad that we
cannot use RAII with BCC 5.6:(


//snippets.cpp:

#include <stdexcept>
#include <memory>
#include <iostream>

using namespace std;

class A
{
public:
A() { cout << "A constructed.\n"; }
~A() { cout << "A destructed.\n"; }

};

int main()
{
try
{
auto_ptr<A> apA(new A);
throw logic_error("Too bad!");
}
catch (exception& e)
{
cout << "Exception cought: " << e.what() << "\n";
}

return 0;
}
///////////////////////////////////////////////////////////////////
bcc32 snippets.cpp:

Borland C++ 5.6.4 for Win32 Copyright (c) 1993, 2002 Borland
snippets.cpp:
Turbo Incremental Link 5.64 Copyright (c) 1997-2002 Borland
///////////////////////////////////////////////////////////////////

snippets.exe:
A constructed.
A destructed.
Exception cought: Too bad!

///////////////////////////////////////////////////////////////////

Jul 23 '05 #8

Moskvichev Nikolay
P: n/a
Serge Skorokhodov (216716244) wrote:

Today all examples (including a new one that is a VCL application) start
to work. The real application I've encountered the problem in does not:(
Neither IDE options nor direct editing of option source nor pragma
option -RT -x -xd at all possible locations does not work:(


Kill .csm .tds , etc. i.e. build from scratch. Sometimes helps.
Jul 23 '05 #9

Serge Skorokhodov (216716244)
P: n/a
Moskvichev Nikolay wrote:
Serge Skorokhodov (216716244) wrote:

Today all examples (including a new one that is a VCL application)
start to work. The real application I've encountered the problem in
does not:( Neither IDE options nor direct editing of option source nor
pragma option -RT -x -xd at all possible locations does not work:(


Kill .csm .tds , etc. i.e. build from scratch. Sometimes helps.


Thanks, the problem is pinpointed to the following
(boost::smart_ptr is heavily involved in real code):

A* p = getGlobalPtrToA();

if ( p ) try
{
//lots of auto_ptrs temporaries created
}
catch ( ... )
{
//some code
}

everything is cleaned up as expected in case of an exception is
raised.

if ( A* p = getGlobalPtrToA() ) try
{
//lots of auto_ptrs temporaries created
}
catch ( ... )
{
//some code
}

unpredictable behavior i.e. some auto_ptrs are cleaned up in some
cases. But some of local vars and auto_ptrs are never cleaned up:(

That's simple:) Thanks for discussion.
Jul 23 '05 #10

red floyd
P: n/a
Pelle Beckman wrote:
Serge Skorokhodov (216716244) skrev:
#include <stdexcept>
#include <memory>

using namespace std;

class A
{
public:
A() { cout << "A constructed.\n"; }
~A() { cout << "A destructed.\n"; }
};

void main()
{
try
{
auto_ptr<A> apA(new A);
throw logic_error("Too bad!");
}
catch (some_except& e)
{
cout << "Exception cought: " << e.what() << "\n";
}
}

The output:

[redacted]

This is actually more of a question:
Won't this code always throw an exception? and
(in later posts) Serge says the output is:
> martin@ubuntu:~$ ./hello
> A constructed.
> A destructed.
> Exception cought: Too bad!
> martin@ubuntu:~$


Shouldn't this be (I really ought to compile this myself and see).
> A constructed.
> Exception caught.
> A destructed

I don't think so. I don't have chapter&verse from the Standard, but I
believe the exception handler is executed *after* the try block is exited.
Jul 23 '05 #11

Serge Skorokhodov (216716244)
P: n/a
red floyd wrote:

<skipped>

This is actually more of a question:
Won't this code always throw an exception? and
(in later posts) Serge says the output is:
martin@ubuntu:~$ ./hello
A constructed.
A destructed.
Exception cought: Too bad!
martin@ubuntu:~$


Shouldn't this be (I really ought to compile this myself and see).
A constructed.
Exception caught.
A destructed

I don't think so. I don't have chapter&verse from the Standard, but I
believe the exception handler is executed *after* the try block is exited.


I've pinpoited the problem to the difference in style
i.e whether a smart pointer is declared out of if statement or
within the if statement:(

The original code where I ran into this problem uses
boost::smart_ptr heavily. It seems to be related to the
boost::smart_ptr except the sample works fine with several
compilers other than bcc32 5.6.4:(

I attached the examples for info, please note that boosttest.cpp
and boosttest2.cpp work fine with Borland C++ 5.6.4 while
boosttest1.cpp does not (the shared_ptr that is allocated within
the try block is not cleaned up).

I've sent the message to boost users list and the decision is
that it is a strange borland bug (so far).

Thanks for discussing the problem.


Jul 23 '05 #12

This discussion thread is closed

Replies have been disabled for this discussion.