I have a struct which contains a static member variable. One of the lines in the destructor of this member variable is allegedly never executed, this is reported by lcov. This one line should only be executed when the program terminates. So I guess its just lcov that isn't able to count it. Valgrind can reveal that the line is of course executed as expected.
Is it possible to have lcov count this line?
Here is the code:
#include <cstdint> // uintX_t
#include <map> // std::map
#include <deque> // std::deque
struct foo
{
struct bar
{
uint8_t* p;
bar(uint8_t* const p_in) : p(p_in) {}
~bar()
{
if (p != nullptr)
{
delete[] p; // This line is allegedly never executed, reported by lcov
}
}
bar(const bar&) = delete;
bar& operator=(const bar&) = delete;
uint8_t* get_p()
{
uint8_t* const tmp = p;
p = nullptr;
return tmp;
}
};
static std::map<uint64_t, std::deque<bar>> storage;
const uint32_t N;
uint8_t* P;
foo(const uint32_t n) : N(n)
{
if (storage[N].size() == 0)
{
P = new uint8_t[N];
}
else
{
uint8_t* const p = storage[N].back().get_p();
storage[N].pop_back();
P = p;
}
}
~foo()
{
storage[N].emplace_back(P);
}
};
std::map<uint64_t, std::deque<typename foo::bar>> foo::storage;
int main()
{
for (int i = 0; i < 2; ++i)
{
foo a(3);
foo b(3);
}
}
gcov/lcov is not able to monitor the construction and destruction of static member variables (and global variables). They are however able to monitor static variables in functions.