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

  • Thread starter Serge Skorokhodov (216716244)
  • Start date
S

Serge Skorokhodov (216716244)

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?
 
W

wittempj

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:~$
 
S

Serge Skorokhodov (216716244)

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.
 
O

Old Wolf

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).
 
S

Serge Skorokhodov (216716244)

Old said:
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:(
 
M

Moskvichev Nikolay

Serge said:
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!

///////////////////////////////////////////////////////////////////
 
P

Pelle Beckman

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
 
S

Serge Skorokhodov (216716244)

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:(
 
M

Moskvichev Nikolay

Serge said:
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.
 
S

Serge Skorokhodov (216716244)

Moskvichev said:
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.
 
R

red floyd

Pelle said:
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.
 
S

Serge Skorokhodov (216716244)

red floyd wrote:

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top