1
我有一個包含靜態成員變量的結構體。據稱這個成員變量的析構函數中的一行從未被執行,這是由lcov報告的。這一行只能在程序終止時執行。所以我猜它只是lcov,不能算數。 Valgrind可以揭示這條線當然是按預期執行的。lcov報告不執行靜態成員變量析構函數中的行
可以讓lcov統計這一行嗎?
下面是代碼:
#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);
}
}