473,387 Members | 1,520 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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

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
11 6060
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
wi******@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
> 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
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Flame Watcher | last post by:
I have a project that uses VCL with Tee chart support and other external controls. There are quite a few persnoal reasons for converting this over to visual.net and would like to know if it is...
17
by: Ziggi | last post by:
Hi. I want to get a C++ IDE, but I dont know whether to go for Bill Gate's solution or Borland's. Could any kind folks detail the relative strength and weaknesses of both, and also tell me which...
15
by: Chris | last post by:
I am just beginning programming again and need a bit of advice. I have both Visual C++ 6.0 Standard Edition and Borland C++ Builder 6. Of these two which do you consider the best for programming...
24
by: serdar | last post by:
Hi. Does anybody say that what is better borland c++ or visual c++? Which compiler does have more help?
1
by: crash | last post by:
Hello, I need to connect to a DB2 database from Borland C++ Builder 6.0. I have used the core labs components for MSSQL and are very happy with them, however I don't believe that they have one...
0
by: Xproblem | last post by:
FTP Client Engine for C/C++ 2.4 Screenshot - Soft.comFTP Client Engine for C/C++ 2.4. ... System Requirements: Windows C/C++ compiler - Microsoft operating system: Windows 95, Windows 98, Windows...
2
by: Pav | last post by:
Hi I want to draw a bar chart in my program written in BCB. My question is: which functions should I use to add values to my chart (and then draw it)? Is there someone who could explain me it...
47
by: =?Utf-8?B?ZW1hdmlzdQ==?= | last post by:
Dear guys, I'm in trouble having to port my project from C++Builder6 to VisualC++. Has anyone of you idea if there are any tools to help my doing this job? My current project is widely using VCL...
7
by: SteelNetNob | last post by:
I have created a COM callable DLL in C# .NET 2.0 and created a TLB from the assembly using the .NET regasm tool. In Borland C++ Builder 4.0 I go to Project->Import Type Library-> and find my...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.