Re: Assertion vs Exception Handling
On Mar 16, 10:13 pm, "Leigh Johnston" <le...@i42.co.uk> wrote:
"James Kanze" <james.ka...@gmail.com> wrote in message
[...]
To be taken seriously please post a VC++ testcase which
illustrates this problem (dtor not being called with
optimizations turned on) and post a defect report on Microsoft
Connect if you have not already.
http://connect.microsoft.com/VisualStudio/feedback/details/336316/missing-destructor-calls-when-optimization-is-enabled
I'm not the first to have encountered it.
I always compile with optimizations turned on and have not
suffered from this with VC9 at least.
It depends somewhat on your coding style. I'd never have
encountered in my own code, either, because I rigorously use
SESE. But the code in which I encountered it is perfectly legal
C (and arguably a case where not using SESE is valid).
My example code was:
----- CompilerError.cpp -----
#include <iostream>
#include <stdlib.h>
class Tracker
{
public:
static int instance_count ;
#define TRACK(f) std::cout << "Tracker::" #f << "(), this = " << this
<< std::endl;
Tracker() { TRACK(ctor); ++ instance_count ;}
Tracker(Tracker const& ) { TRACK(copy); ++ instance_count ;}
~Tracker() { TRACK(dtor); -- instance_count; }
Tracker const& operator=(Tracker const& ) { TRACK(asgn); return
*this; }
#undef TRACK
};
int Tracker::instance_count = 0;
Tracker f()
{
for ( int i = 0; i < 2; ++ i )
{
std::cout << i << ": start" << std::endl;
Tracker t;
if ( i != 0 )
{
std::cout << i << ": returning" << std::endl;
return t;
}
std::cout << i << ": end" << std::endl;
}
std::cerr << "I don't believe it" << std::endl;
abort();
}
void g()
{
Tracker t(f());
std::cout << "After f()" << std::endl;
}
int
main()
{
g();
std::cout << Tracker::instance_count << " remaining Trackers" <<
std::endl;
return 0;
}
----- CompilerError.cpp -----
(Copy pasted from my original posting in
microsoft.public.vc.language. I hope the neither Windows nor
Google introduce any anomalies.)
--
James Kanze