MWE
#include <iostream>
struct Foo {
Foo() {
std::cout << "Constructing Foo " << this << std::endl;
}
~Foo() {
std::cout << "Destructing Foo " << this << std::endl;
}
};
Foo global_foo;
int main () {
std::cout << "Entering and exiting main()" << std::endl;
return 0;
}
The problem
Compile the above with options -fprofile-arcs -ftest-coverage, runn the program, and then run gcov. The program output clearly shows that Foo::Foo(), main(), and Foo::~Foo() are called, in that order. The gcov output shows that Foo::Foo() and main() are called, but not Foo::~Foo().
Root cause
The global objects are destroyed by a GNU internal exit handler (function registered with at_exit()). The final gcov stats are produced by another exit handler. The gcov exit handler is obviously called before the global destruction exit handler, so gcov doesn't see the destructors being called.
Bug status
This is an old, old bug in gcov. Here's the Bugzilla link: http://gcc.gnu.org/bugzilla/show_bug.cgi?id=7970. The bug still exists nine years later, at least in i686-apple-darwin10-g++-4.2.1.
The question
Is this an unresolvable bug in gcov, something I have to live with, or is it just something that happened to slip through the cracks (nine years old and utterly forgotten)? If the latter, how to fix it?