BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Please confirm if BORLAND BUG
Goto page 1, 2  Next
 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (Language C++)
View previous topic :: View next topic  
Author Message
Sally
Guest





PostPosted: Wed Aug 03, 2005 9:57 am    Post subject: Please confirm if BORLAND BUG Reply with quote



The following code seems to have a Borland Bug in it, or is it that I'm
doing something that is not officially "allowed"? Then again, I can't
imagine why NESTED try/finally wouldn't be allowed within a finally
loop.

SITUATION: just 1 exception thrown, finally block has try-finally blocks
inside it nested 2 deep.
EXPECTATION: test1 and test2 should end up 0 (test1-- and test2-- both
run), imagine they are locks
PROBLEM: test2-- never runs, the function exits to the caller's
exception handlers right after "test1--"


_______________________________________
void __fastcall testForBorlandBug(void) {
long ignored=0;
long test1 = 0;
long test2 = 0;

try {
throw Eanything;
} __finally {

test2++;
try {
test1++;
try {
ignored++;
} __finally {
test1--;
}

} __finally {
test2--;
}

}
}



Back to top
Vladimir Stefanovic
Guest





PostPosted: Wed Aug 03, 2005 11:09 am    Post subject: Re: Please confirm if BORLAND BUG Reply with quote




'__finally' is not a C++ keyword (it's just an extension), and
it has many issues that has already been reported. Most of the
problems come from nested try/finally statements.

I have alredy shown some cases in these NGs when try/finally
behaves unexpectedly.

You can consider redesigning your code or use the RAII design
pattern to secure some 'finally' tasks to be guaranteed
executed....



"Sally" <nospamforme (AT) spamme (DOT) com> wrote:
Quote:
The following code seems to have a Borland Bug in it, or is it that I'm
doing something that is not officially "allowed"? Then again, I can't
imagine why NESTED try/finally wouldn't be allowed within a finally
loop.

SITUATION: just 1 exception thrown, finally block has try-finally blocks
inside it nested 2 deep.
EXPECTATION: test1 and test2 should end up 0 (test1-- and test2-- both
run), imagine they are locks
PROBLEM: test2-- never runs, the function exits to the caller's
exception handlers right after "test1--"


_______________________________________
void __fastcall testForBorlandBug(void) {
long ignored=0;
long test1 = 0;
long test2 = 0;

try {
throw Eanything;
} __finally {

test2++;
try {
test1++;
try {
ignored++;
} __finally {
test1--;
}

} __finally {
test2--;
}

}
}





Back to top
Vladimir Stefanovic
Guest





PostPosted: Wed Aug 03, 2005 11:20 am    Post subject: Re: Please confirm if BORLAND BUG Reply with quote




Also, look here:
http://tinyurl.com/9god4


"Sally" <nospamforme (AT) spamme (DOT) com> wrote:
Quote:
The following code seems to have a Borland Bug in it, or is it that I'm
doing something that is not officially "allowed"? Then again, I can't
imagine why NESTED try/finally wouldn't be allowed within a finally
loop.

SITUATION: just 1 exception thrown, finally block has try-finally blocks
inside it nested 2 deep.
EXPECTATION: test1 and test2 should end up 0 (test1-- and test2-- both
run), imagine they are locks
PROBLEM: test2-- never runs, the function exits to the caller's
exception handlers right after "test1--"


_______________________________________
void __fastcall testForBorlandBug(void) {
long ignored=0;
long test1 = 0;
long test2 = 0;

try {
throw Eanything;
} __finally {

test2++;
try {
test1++;
try {
ignored++;
} __finally {
test1--;
}

} __finally {
test2--;
}

}
}





Back to top
Andrue Cope [TeamB]
Guest





PostPosted: Wed Aug 03, 2005 11:36 am    Post subject: Re: Please confirm if BORLAND BUG Reply with quote

Don't use __finally. It is a proprietary extension so it does whatever
Borland think it should do. Historically it has suffered many bugs and
BCB4 will certain contain a number of them.

The C++ language doesn't actually need a __finally construct. The job
can be done using the RAII paradigm. The language guarantees that any
object created on the stack will be automatically destroyed when the
stack unwinds. For a class this means guaranteed calling of the ctor
and dtor.

Your example is of course contrived but I think the following would be
the RAII equivalent. If nothing else it would be worth stepping through
the code line by line to see what happens. In particular note what
happens at the '}'s.

class TIncDec
{
long & Counter;
public:
TIncDec( long & TheCounter )
: Counter( TheCounter )
{
++Counter; // NB:Putting '++' in front may be more efficient
}

~TIncDec()
{
--Counter;
}
};

void __fastcall testForBorlandBug(void)
{
long ignored=0,
test1=0,
test2=0;

TIncDec id2( test2 );

{
TIncDec id1( test1 );

{
++ignored;
}
}
}
--
Andrue Cope [TeamB]
[Bicester, Uk]
http://info.borland.com/newsgroups/guide.html
Back to top
Serge Skorokhodov
Guest





PostPosted: Wed Aug 03, 2005 3:10 pm    Post subject: Re: Please confirm if BORLAND BUG Reply with quote

Vladimir Stefanovic wrote:

<skip>
Quote:

You can consider redesigning your code or use the RAII design
pattern to secure some 'finally' tasks to be guaranteed
executed....

RAII dosn't always work properly with bcb32:( See the attached

code snippet (sorry is slitely longer than 50 lines as it
contains both error and workaround). Frankly, it requires
boost::smart_ptr though and I failed to reproduce it without boost.

If conditional is set to 1 then destructor for the line

auto_ptr<A> ap(new A("auto_ptr"));

is not called:(

#include <string>
#include <iostream>
#include <memory>
#include "boost/smart_ptr.hpp"
using namespace std;
using namespace boost;
class A
{
public:
A(const char* msg);
~A();
int get_a() const
{
throw a;
return a;
}

private:
int a;
string msg_;
};

A::A(const char* msg) : a(10), msg_(msg)
{
cout << "A::A() is called with msg: " << msg_ << endl;
}

A::~A()
{
cout << "A::~A() is called with msg: " << msg_ << endl;
}

int main()
{

shared_ptr weak_ptr<A> wptrans = spsrc;
#if 1
if ( shared_ptr<A> spint = wptrans.lock() ) try
{
#else
shared_ptr<A> spint;
if ( spint = wptrans.lock() ) try
{
#endif
auto_ptr<A> ap(new A("auto_ptr"));
cout << "Access to A through spint: " << spint->get_a()
<< endl;
}
catch ( int e )
{
cout << "exception caught from spint: " << e << endl;
}
return 0;
}



--
Serge

Back to top
Alisdair Meredith [TeamB]
Guest





PostPosted: Wed Aug 03, 2005 3:42 pm    Post subject: Re: Please confirm if BORLAND BUG Reply with quote

Serge Skorokhodov wrote:

Quote:
RAII dosn't always work properly with bcb32:( See the attached
code snippet (sorry is slitely longer than 50 lines as it
contains both error and workaround).

BCB has several problems with destructors and the 'enable inline
function expansion' compiler option. You should always disable this
option, even in release builds.

There are several bug reports on this issue in Quality Central, and I
fervantly hope they will be fixed when the next version of BCB ships
(codename DeXter)

However, no matter how many RAII bugs you find, __finally remains an
inferior option as

i/ all RAII bugs I am aware of also apply to __finally
ii/ __finally is non-standard, and without clearly defined semantics it
is not clear that there really IS a bug that Borland must fix
iii/ as it is non-standard, it also binds your code to the Borland BCB
compiler for the future.

I do believe in using the BCB compiler extensions when there is a
proven advantage, which largely comes down to using the VCL.

I will avoid them entirely when there is no need, and I can never see a
need for __finally. [With this exception, Borland did a really good
job of limitting the number of compiler extensions needed to support
VCL. Compare that list against MS extensions in C++/CLI for instance.]

AlisdairM(TeamB)

Back to top
Sally
Guest





PostPosted: Wed Aug 03, 2005 9:24 pm    Post subject: Re: Please confirm if BORLAND BUG Reply with quote

Thank you for the several confirmations that this is a Borland Bug. Bug
can anyone tell me if THIS bug is still in the newest version of
C++Builder (I believe CB6?)

Basically, my current workaround is to not use more than 1 level of
nesting WITHIN a finally loop. Not ideal but...

Frankly the RAII method is reliable but tediously horrible. I have about
2000 finally blocks in my program, and if I had to create objects for
each and every one I would go crazy. Particularly when you consider it
is much harder to read back code with RAII.

I have found several __finally bugs (most annoying is that they don't
always run when you use "return" statements)... but I'd still rather
work around all the bugs than switch to RAII.

Blah, Borland, FIX THE __FINALLY STUFF *PLEASE*.


Back to top
Vladimir Stefanovic
Guest





PostPosted: Thu Aug 04, 2005 4:39 am    Post subject: Re: Please confirm if BORLAND BUG Reply with quote

Quote:
Frankly the RAII method is reliable but tediously horrible.
I have about 2000 finally blocks in my program, and if I
had to create objects for each and every one I would go
crazy. Particularly when you consider it is much harder
to read back code with RAII.

In depends on your concrete needs, but in such situation,
template classes ca be ideal. Thus you may write the one or
few template RAII classes to handle most of your clearance
code...

Also look at STL way of creating objects which already
has RAII implemented in itself...




--
Best regards,
Vladimir Stefanovic



Back to top
Andrue Cope [TeamB]
Guest





PostPosted: Thu Aug 04, 2005 8:12 am    Post subject: Re: Please confirm if BORLAND BUG Reply with quote

Sally wrote:

Quote:
Frankly the RAII method is reliable but tediously horrible. I have
about 2000 finally blocks in my program, and if I had to create
objects for each and every one I would go crazy.

You probably won't have to.

I have been using RAII for a couple of years now and I've only written
half a dozen general RAII classes. For deallocation std::auto_ptr and
occasionally boost::scoped_array come into play. An example:

bool FormAddSource_NewShow(
TFormAddSource_DeviceList *& DeviceList,
bool AllowPreworkedF )
{
std::auto_ptr<TfrmAddSource>
frmAddSource( new TfrmAddSource( DeviceList,
AllowPreworkedF ) );

return ( frmAddSource->ShowModal()==mrOk );
}

Once you get into it you start to put every object on the stack and
that gives you the benefits of RAII as well.

--
Andrue Cope [TeamB]
[Bicester, Uk]
http://info.borland.com/newsgroups/guide.html

Back to top
Hendrik Schober
Guest





PostPosted: Thu Aug 04, 2005 9:48 am    Post subject: Re: Please confirm if BORLAND BUG Reply with quote

Sally <nospamforme (AT) spamme (DOT) com> wrote:
Quote:
[...]
Frankly the RAII method is reliable but tediously horrible. I have about
2000 finally blocks in my program, and if I had to create objects for
each and every one I would go crazy.

Look at this:
http://www.cuj.com/documents/s=8000/cujcexp1812alexandr/alexandr.htm

Quote:
Particularly when you consider it
is much harder to read back code with RAII.

I don't understand this.

Quote:
I have found several __finally bugs (most annoying is that they don't
always run when you use "return" statements)... but I'd still rather
work around all the bugs than switch to RAII.

Blah, Borland, FIX THE __FINALLY STUFF *PLEASE*.


Use RAII. RAII will even work should Borland
ever pull the plug on their C++ compilers.
Also, if they are sane, then they will fix
std-related bugs (those that affect RAII)
before they fix any bug with proprietary stuff
like '__finally'.

Schobi

--
[email]SpamTrap (AT) gmx (DOT) de[/email] is never read
I'm Schobi at suespammers dot org

"Coming back to where you started is not the same as never leaving"
Terry Pratchett



Back to top
Chris Uzdavinis (TeamB)
Guest





PostPosted: Thu Aug 04, 2005 3:06 pm    Post subject: Re: Please confirm if BORLAND BUG Reply with quote

"Sally" <nospamforme (AT) spamme (DOT) com> writes:

Quote:
Frankly the RAII method is reliable but tediously horrible.

I have to disagree.

Quote:
I have about 2000 finally blocks in my program, and if I had to
create objects for each and every one I would go crazy.

You have to write __finally blocks for each and every one, and
assuming your RAII object were alread written, then declaring one is
less typing than the __finally block, and runs more efficiently to
boot.

There are usually only a few cases of variation between RAII needs
within an application. If you write the object, write it once and
place it in your library of useful routines. You can reuse it over
and over.

Quote:
Particularly when you consider it is much harder to read back code
with RAII.

I don't consider that harder at all. In fact, when you get
complicated nesting, excetions, and finally blocks, there is a
tendency to duplicate code, and have to do a lot more work to set
deleted pointers to 0, etc. I find a screenfull of catch blocks, or
finally blocks, to be much less readable than a few well-named RAII
object delcarations.

Quote:
I have found several __finally bugs (most annoying is that they don't
always run when you use "return" statements)... but I'd still rather
work around all the bugs than switch to RAII.

The effort of switching would be admittedly big. But had you written
RAII in the first place, it wouldn't be an issue. (And your code
would compile on all C++ compilers, rather than just on Borland's.)

Quote:
Blah, Borland, FIX THE __FINALLY STUFF *PLEASE*.

I hope they do. However, whether or not they actually fix it, you are
demonstrating the very reason why I suggest using RAII and
standard-compliant (portable) code: you are depending on a single
vendor's implentation, and the correctness of your application is
solely determined by Borland's willingness to fix a bug that is
hitting you (and/or your willingness to implement a workaround.)

Had you avoided proprietary extensions, you would not be in such a
bind. This alone gives the RAII solution a huge advantage. Even if
there may be some reasons to not like RAII, the benefits are far
greater to use it than not.

--
Chris (TeamB);

Back to top
Chris Uzdavinis (TeamB)
Guest





PostPosted: Thu Aug 04, 2005 3:07 pm    Post subject: Re: Please confirm if BORLAND BUG Reply with quote

"Hendrik Schober" <SpamTrap (AT) gmx (DOT) de> writes:

Quote:
Also, if they are sane, then they will fix
std-related bugs (those that affect RAII)
before they fix any bug with proprietary stuff
like '__finally'.

Well, it could be argued that the proprietary extensions are what make
Borland's compiler "compelling". Without the VCL, it's just a "plain"
C++compiler, which is starting to become a commodity item.

--
Chris (TeamB);

Back to top
Hendrik Schober
Guest





PostPosted: Thu Aug 04, 2005 3:22 pm    Post subject: Re: Please confirm if BORLAND BUG Reply with quote

Chris Uzdavinis (TeamB) <chris (AT) uzdavinis (DOT) com> wrote:
Quote:
"Hendrik Schober" <SpamTrap (AT) gmx (DOT) de> writes:

Also, if they are sane, then they will fix
std-related bugs (those that affect RAII)
before they fix any bug with proprietary stuff
like '__finally'.

Well, it could be argued that the proprietary extensions are what make
Borland's compiler "compelling". [...]


Right. Nested '__finally' however, aren't
all that compelling, are they?

Schobi

--
[email]SpamTrap (AT) gmx (DOT) de[/email] is never read
I'm Schobi at suespammers dot org

"Coming back to where you started is not the same as never leaving"
Terry Pratchett



Back to top
Chris Uzdavinis (TeamB)
Guest





PostPosted: Thu Aug 04, 2005 5:07 pm    Post subject: Re: Please confirm if BORLAND BUG Reply with quote

"Hendrik Schober" <SpamTrap (AT) gmx (DOT) de> writes:

Quote:

Right. Nested '__finally' however, aren't
all that compelling, are they?


Not to me. However, it's in Borland's best interst to keep those
features working perfectly so customers who depend on them don't
struggle to break free from vendor lock-in.

--
Chris (TeamB);

Back to top
Ed Mulroy
Guest





PostPosted: Thu Aug 04, 2005 7:28 pm    Post subject: Re: Please confirm if BORLAND BUG Reply with quote

The debugger and a usable IDE also make it compelling. Back when Borland
C++ was current the project manager was also a compelling item, very
powerful and not equaled by anyone since.

.. Ed

Quote:
Chris Uzdavinis wrote in message
news:j5ll3hhha5.fsf (AT) explicit (DOT) atdesk.com...

Well, it could be argued that the proprietary extensions are
what make Borland's compiler "compelling". Without the
VCL, it's just a "plain" C++compiler, which is starting to
become a commodity item.



Back to top
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (Language C++) All times are GMT
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.