473,396 Members | 1,605 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,396 software developers and data experts.

c++builder exceptions

Hi

I wrote a C++Builder program that throws/catch many exceptions
(let's say 1.000.000/ hours). The problem is that I think that is a
bug in the Borland C++Builder exception handling mechanism.

If I catch an exception with catch(...) the memory is never freed! so
my computer can quickly get memory-exhausted.

Here is the code that generates the problem. In VC++ I don't have this
problem. Neither Delphi. I've managed to solve it somehow (as shown in
the second part), but I think that this is not a solution, mainly
because the program is slower in the second case and I'm not sure that
I catch all exceptions.

Does anybody else has another solution to my problem?

thanks,
mihai
#include <vcl.h>
#pragma hdrstop
//-------------------------------------------------------------------
--------
#pragma argsused
int main(int argc, char* argv[])
{
int a = 4;
int b = 0;
int c;

// the case where the memory is not freed
for (int i = 0; i < 1000000; i++)
try{
c = a/b;
}
catch(...){ // with this I catch all exceptions
c = 1;
}

/*
// the case when the memory is freed
for (int i = 0; i < 1000000; i++)
try{
c = a/b;
}
catch(Exception &e){
c = 1;
e.Free(); //Borland says "never call Free()"!
}
*/
return 0;
}

//-------------------------------------------------------------------
--------
Jul 22 '05 #1
5 6140

"Mihai Oltean" <mi*********@yahoo.com> wrote in message
news:4e**************************@posting.google.c om...
Hi

I wrote a C++Builder program that throws/catch many exceptions
(let's say 1.000.000/ hours). The problem is that I think that is a
bug in the Borland C++Builder exception handling mechanism.

If I catch an exception with catch(...) the memory is never freed! so
my computer can quickly get memory-exhausted.

Here is the code that generates the problem. In VC++ I don't have this
problem. Neither Delphi. I've managed to solve it somehow (as shown in
the second part), but I think that this is not a solution, mainly
because the program is slower in the second case and I'm not sure that
I catch all exceptions.

Does anybody else has another solution to my problem?

thanks,
mihai
#include <vcl.h>
#pragma hdrstop
//-------------------------------------------------------------------
--------
#pragma argsused
int main(int argc, char* argv[])
{
int a = 4;
int b = 0;
int c;

// the case where the memory is not freed
for (int i = 0; i < 1000000; i++)
try{
c = a/b;
}
catch(...){ // with this I catch all exceptions
c = 1;
}

/*
// the case when the memory is freed
for (int i = 0; i < 1000000; i++)
try{
c = a/b;
}
catch(Exception &e){
c = 1;
e.Free(); //Borland says "never call Free()"!
}
*/
return 0;
}


I think you are not going to get an answer to this problem unless you post
some compilable code.

What is <vcl.h>? It's not part of standard C++. What is Exception? Is it
defined in <vcl.h>? If these two are important to your problem, then you
should ask on a group where they are topical, presumably a Borland group.

Also you should be aware that division by zero does not cause an exception,
it causes undefined behaviour. So if in your real program you are doing a
divide by zero, then the Borland compiler can do what it likes and you have
no reason to complain. It's not a bug, its a feature of C++.

john
Jul 22 '05 #2
Mihai Oltean wrote:
Hi

I wrote a C++Builder program that throws/catch many exceptions
(let's say 1.000.000/ hours). The problem is that I think that is a
bug in the Borland C++Builder exception handling mechanism.

If I catch an exception with catch(...) the memory is never freed! so
my computer can quickly get memory-exhausted.

Here is the code that generates the problem. In VC++ I don't have this
problem. Neither Delphi. I've managed to solve it somehow (as shown in
the second part), but I think that this is not a solution, mainly
because the program is slower in the second case and I'm not sure that
I catch all exceptions.

Does anybody else has another solution to my problem?

thanks,
mihai
#include <vcl.h>
This is a non-standard header.
#pragma hdrstop
//-------------------------------------------------------------------
--------
#pragma argsused
These preprocessor directives are specific to a
particular implementation. Hence ignoring it.

int main(int argc, char* argv[])
{
We dont use either of them. So never mind to put them here.
int a = 4;
int b = 0;
int c;

// the case where the memory is not freed
for (int i = 0; i < 1000000; i++)
Array indices would better be defined as size_t .
What about the opening brace to signify the beginning
of the block, belonging to this loop ?
try{
c = a/b;


This would result in a divide-by-zero error.
I am not really sure if dividing by zero, results in an
error or an exception.


<-- Code Begins -- >
#include <cstdlib> //EXIT_SUCCESS , system
#include <iostream> //cout, endl
#include <stdexcept> //logic_error

int mydiv(int num, int den);
int main() {
int a = 4;
int b = 0;
int c;

// the case where the memory is not freed
for (size_t i = 0; i < 1000000; i++) {

try {
// c = a/b; // Let me replace it with my custom function
c = mydiv(a, b);
} catch(...){ // with this I catch all exceptions
c = 1;
}
}
std::cout << "Out of the loop " << std::endl;
system("pause");
return EXIT_SUCCESS;
}

int mydiv(int num, int den) {
if (!den) {
throw std::logic_error("Divide by zero");
} else {
return ( num / den ) ;
// Crude integer division that is.
}
}

<-- Code Ends -->

This code ran successfully , in my implementation.
The exception handlers were invoked 1000000 times ( the
same upper bound specified for the loop).

<OT>
I run MinGW.
gcc (GCC) 3.4.2 (mingw-special)
</OT>

--
Karthik.
Jul 22 '05 #3
On 6 Oct 2004 23:18:20 -0700, mi*********@yahoo.com (Mihai Oltean)
wrote:
If I catch an exception with catch(...) the memory is never freed! so
my computer can quickly get memory-exhausted.


This is definitely off-topic in comp.lang.c++, but I will give you the
answer because it is short, and you may have trouble finding it
elsewhere.

This is a known issue with Borland. The solution is: Don't ever use
catch(...). Specify the type of exception, e.g.:

catch(const std::exception &e)
// any of the STL containers might throw this
// or an exception derived from std::exception

or for SEH-type exceptions, which are wrapped by the VCL as Exception:

catch(Exception &e)
// note that you cannot, for some reason,
// catch a const Exception & because of the way
// Borland implements it.

HTH

--
Bob Hairgrove
No**********@Home.com
Jul 22 '05 #4
Thanks Bob,

The problem if I use:

catch(Exception &e)

is that I have to call e.Free() at the end of catch block. Otherwise
the memory will not be freed. And Borland does not say anything about
that. But if I throw the exception myself and then try to catch it I
don't have to use e.Free() because I get an error!

regards,
mihai

Bob Hairgrove <in*****@bigfoot.com> wrote in message news:<ph********************************@4ax.com>. ..
On 6 Oct 2004 23:18:20 -0700, mi*********@yahoo.com (Mihai Oltean)
wrote:
If I catch an exception with catch(...) the memory is never freed! so
my computer can quickly get memory-exhausted.


This is definitely off-topic in comp.lang.c++, but I will give you the
answer because it is short, and you may have trouble finding it
elsewhere.

This is a known issue with Borland. The solution is: Don't ever use
catch(...). Specify the type of exception, e.g.:

catch(const std::exception &e)
// any of the STL containers might throw this
// or an exception derived from std::exception

or for SEH-type exceptions, which are wrapped by the VCL as Exception:

catch(Exception &e)
// note that you cannot, for some reason,
// catch a const Exception & because of the way
// Borland implements it.

HTH

Jul 22 '05 #5
I'm extremely new here and not done much to speak of with windows C++
but seems you speak of the same Borland Builder that I'm playing with.
I've just finished the Tutorial on making their small Editor. Yes,
finished and flunked it because they give a bad line in the Save
Command. I've dropped them a line but......???? Did you find the
answer to this one?

I've done a lot of work with VB so have some experience with that and
C and such for years off and on plus a lot of C++ for dos which simply
doens't hit it any longer with XP and on.

Verne
On 7 Oct 2004 23:55:02 -0700, mi*********@yahoo.com (Mihai Oltean)
wrote:
Thanks Bob,

The problem if I use:

catch(Exception &e)

is that I have to call e.Free() at the end of catch block. Otherwise
the memory will not be freed. And Borland does not say anything about
that. But if I throw the exception myself and then try to catch it I
don't have to use e.Free() because I get an error!

regards,
mihai

Bob Hairgrove <in*****@bigfoot.com> wrote in message news:<ph********************************@4ax.com>. ..
On 6 Oct 2004 23:18:20 -0700, mi*********@yahoo.com (Mihai Oltean)
wrote:
>If I catch an exception with catch(...) the memory is never freed! so
>my computer can quickly get memory-exhausted.


This is definitely off-topic in comp.lang.c++, but I will give you the
answer because it is short, and you may have trouble finding it
elsewhere.

This is a known issue with Borland. The solution is: Don't ever use
catch(...). Specify the type of exception, e.g.:

catch(const std::exception &e)
// any of the STL containers might throw this
// or an exception derived from std::exception

or for SEH-type exceptions, which are wrapped by the VCL as Exception:

catch(Exception &e)
// note that you cannot, for some reason,
// catch a const Exception & because of the way
// Borland implements it.

HTH


Jul 22 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Anand K Rayudu | last post by:
Hi All, I am new to python & want to build some GUI dialogs. Can some one please suggest some document reference. I am hoping that standard python install will have some GUI development...
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: Mike Turco | last post by:
I like using the expression builder for a lot of different things but it isn't always available when I want to use it, for example in the code window, or in all of the control properties. I am...
0
by: FluffyCat | last post by:
Last fall I started a series of design pattern examples using PHP5. I think the last pattern I did was the Singleton in January. Getting back to it here is my first cut at the Builder pattern. ...
2
by: sicapitan | last post by:
FYI, with something like this: hotelement = "hotelement"+hotcount; hotspot = Builder.node('div', { id:'hotspot'+hotcount, className:'hotspot', style:''+divstyle }, ) ]);
7
by: simonZ | last post by:
I have array variable transfer: String transfer which has some data Than I would like to convert this data into string: Which way is more efficient: StringBuilder rezult=new StringBuilder(); ...
8
by: EddieTH | last post by:
Hello everyone, I am new to this forum so I'll give my greetings to everyone! I am using oracle forms builder and reports builder for 1 year now and I have a problem I cannot seem to be able to...
0
by: JosAH | last post by:
Greetings, Introduction Before we start designing and implementing our text builder class(es), I'd like to mention a reply by Prometheuzz: he had a Dutch version of the entire bible ...
2
xarzu
by: xarzu | last post by:
Platform Builder is a tool for building a Windows CE Operating system on your computer and then loading it on a Windows CE device. All this is done through Platform Builder. And I do it all...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.