C++ object lifetime checkers

Hi all,

here is a problem translated from what I encountered in work yesterday:

struct Foo {
   const std::vector<int>& v;
   Foo(const std::vector<int>& v) : v(v) {
     // do some preflighting

     // perform stuff

void bar(int i)
    Foo raii(std::vector<int>(1, i));
    // more stuff

The big setback was that I had several bar-like functions that all received a vector to start with, but this one only had a single integer as input. So I passed that in a temporary vector. Hell broke loose :frowning:

I wonder whether clang does catch this kind of problem (anyway the Foo destructor is visible, inline), or whether it would be possible to implement such an object lifetime check?



Hi Gabor,

The short answer is not right now as the analyzer's C++ support is not quite there. The support to enable these kind of checks is being worked on though. There are a few ways we could find this kind of error.

We are always interested to hear about real-world bugs that you would like to be detected, and I encourage you to file bugs with an example of what you would like to be found (like the example you included :))


I'm always interested in hearing about