Bug 391772 - Memcheck reports conditional jumps if I use raw pointer, but not for std::vector
Summary: Memcheck reports conditional jumps if I use raw pointer, but not for std::vector
Status: REPORTED
Alias: None
Product: valgrind
Classification: Developer tools
Component: memcheck (show other bugs)
Version: 3.12.0
Platform: RedHat Enterprise Linux Linux
: NOR normal
Target Milestone: ---
Assignee: Julian Seward
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2018-03-12 18:55 UTC by Ravi Kappiyoor
Modified: 2018-03-12 18:55 UTC (History)
1 user (show)

See Also:
Latest Commit:
Version Fixed In:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
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.