Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Possible to detect stack allocated object that isn't captured?

Tags:

c++

raii

Is it possible to prevent or detect the following bug, in any way (static analysis), where the stack allocated object is not captured and goes out of scope on the same line that it was constructed?

Resource  resourceA, resourceB;

void someFunction()
{
    ScopedResourceBinder resourceBindA( resourceA );
    ScopedResourceBinder( resourceB ); // <--- BUG
}

The first ScopedResourceBinder is correct, but the second one doesn't do anything, since it immediately 'unbinds' right after it 'binds' (hypothetically speaking).

This is clearly a programmers error, but I have debugged this a few times now (for hours on a couple occassion) and it is extremely difficult to spot. Once you see it you think "ah that was a stupid one", but in practice, it is easy to make the mistake and the compiler is defenseless... or is it?

Background info: I work with a library that makes heavy use RAII class for pushing an popping state, for example OpenGL resources. Managing the bindings with scope is a big improvement of manually calling bind() / unbind() functions, but the potential bug listed here comes up with this new pattern.

like image 209
rich.e Avatar asked Oct 31 '22 20:10

rich.e


1 Answers

ScopedResourceBinder( resourceB ); is the same as ScopedResourceBinder resourceB;, i.e. it declares a named variable with name resourceB, calling the default constructor of ScopedResourceBinder.

This persists until the end of the function, however it is still a bug because it does not carry out your intent of binding to the variable resourceB.

To prevent this particular case you could make sure that ScopedResourceBinder does not have a default constructor.

like image 77
M.M Avatar answered Nov 15 '22 03:11

M.M