Discussion:
Destructor of local variable not being called.
(too old to reply)
James Kanze
2009-12-07 15:16:30 UTC
Permalink
Is this a known bug, and does anyone know a work-around? If a
program returns a local variable defined in a loop, the variable
isn't correctly destructed when the return statement isn't
executed.

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

The bug only occurs when optimization is activated (/O2). As a
work around, it's sufficient to move the variable outside of the
loop, and use assignment in the loop.

--
James Kanze
Vladimir Grigoriev
2009-12-07 16:21:45 UTC
Permalink
I have run the code and I have got that "remaining Tracker" is equal to 0.
What should instance_count be equal in your opinion?

Vladimir Grigoriev
Post by James Kanze
Is this a known bug, and does anyone know a work-around? If a
program returns a local variable defined in a loop, the variable
isn't correctly destructed when the return statement isn't
executed.
----- CompilerError.cpp -----
#include <iostream>
#include <stdlib.h>
class Tracker
{
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 -----
The bug only occurs when optimization is activated (/O2). As a
work around, it's sufficient to move the variable outside of the
loop, and use assignment in the loop.
--
James Kanze
James Kanze
2009-12-07 16:28:05 UTC
Permalink
Post by Vladimir Grigoriev
I have run the code and I have got that "remaining Tracker"
is equal to 0. What should instance_count be equal in your
opinion?
What version of the compiler, and what options. I'm using
Visual Studios 2005 (but collegues have tried it with Visual
Studios 2008 and 2010), and when compiled with /O2, "remaining
Tracker" is 1, and you can trace where the same address has been
constructed twice without an intervening destructor.

--
James Kanze
Igor Tandetnik
2009-12-07 19:22:21 UTC
Permalink
Post by James Kanze
Is this a known bug, and does anyone know a work-around? If a
program returns a local variable defined in a loop, the variable
isn't correctly destructed when the return statement isn't
executed.
Looks like this one:

http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=336316
--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not necessarily a good idea. It is hard to be sure where they are going to land, and it could be dangerous sitting under them as they fly overhead. -- RFC 1925
James Kanze
2009-12-08 08:29:40 UTC
Permalink
Post by James Kanze
Is this a known bug, and does anyone know a work-around? If a
program returns a local variable defined in a loop, the variable
isn't correctly destructed when the return statement isn't
executed.
http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?...
It sure does. (This is more or lesss my first experience with
Visual Studios, so I'm not too familiar with where to go looking
for bug reports and such.)

--
James Kanze

Loading...