Bug 391772

Summary: Memcheck reports conditional jumps if I use raw pointer, but not for std::vector
Product: [Developer tools] valgrind Reporter: Ravi Kappiyoor <ravi.kappiyoor>
Component: memcheckAssignee: Julian Seward <jseward>
Status: REPORTED ---    
Severity: normal CC: ravi.kappiyoor
Priority: NOR    
Version: 3.12.0   
Target Milestone: ---   
Platform: RedHat Enterprise Linux   
OS: Linux   
Latest Commit: Version Fixed In:

Description Ravi Kappiyoor 2018-03-12 18:55:11 UTC
Consider the following code:

#include <functional>
#include <iostream>
#include <vector>

class A
{
public:
  A(int const & val) : test(val) {}

  int getTest() const { return test.get(); }
private:
  std::reference_wrapper<int const> test;
};

std::vector<A> a;

void func()
{
  a.push_back(A{2});
}

int main()
{
  func();

  std::cout << a.back().getTest() << std::endl;
}

This code is compiled with the following: g++ -g -std=c++14 test.cpp -o a.out

Here, the function `func` adds a new object of type `A` to the `std::vector<A>` `a`. However, in doing so, it binds a reference_wrapper to a temporary (`std::reference_wrapper<int const> test` is bound to the integer literal `2`), and, as such, any attempt to use that member variable 
 (such as the print statement in `main`) should result in a memory error. When I run this program under memcheck, however, memcheck reports no errors. If I simply replace `std::vector<A> a;` with `A *a;` (and change `func` to `a = new A(2);`, and the print statement to `std::cout << a->getTest() << std::endl;`), and run that executable with memcheck, I get the expected warnings regarding uninitialized variables. It appears then, that somehow, the use of std::vector is causing memcheck to get confused.