Valgrind false positive in make_shared

Hi Folks
I've been tracking down some almost-certainly-false-positives from
Valgrind. Consider the following code:
#include <iostream>
#include

-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 On Tuesday 16 November 2010, Dave Steffen wrote:
==8198== Conditional jump or move depends on uninitialised value(s) ==8198== at 0x401659: StateSpace::StateSpace(int) (testState.cpp:14) ==8198== by 0x401796: boost::shared_ptr<StateSpace> boost::make_shared
(int&&, ) (make_shared.hpp:150) ==8198== by 0x4016BD: State::State() (testState.cpp:22) ==8198== by 0x401317: main (testState.cpp:29)
Any ideas about why Valgrind doesn't like the call to make_shared?
It looks like it might be a bug in make_shared's perfect forwarding when compiling in c++0x mode. When I compile your example I get a warning: $ g++ -Wall -g -std=c++0x -I ~/svn/boost_trunk/ make_shared_valgrind.cpp /svn/boost_trunk/boost/smart_ptr/make_shared.hpp: In function ‘T&& boost::detail::sp_forward(T&) [with T = int]’: /svn/boost_trunk/boost/smart_ptr/make_shared.hpp:150: instantiated from ‘boost::shared_ptr<X> boost::make_shared(Arg1&&, Args&& ...) [with T = StateSpace, Arg1 = int, Args = ]’ make_shared_valgrind.cpp:21: instantiated from here /svn/boost_trunk/boost/smart_ptr/make_shared.hpp:93: warning: returning reference to temporary
Any ideas about how to silence this warning (other than adding it to Valgrind's suppressions file)?
Not forwarding a literal constant works: class State { public: State() : m_rep(make_shared<StateSpace>(spaceType) ) {} static const int spaceType; boost::shared_ptr<StateSpace> m_rep; }; const int State::spaceType = 0; -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.9 (GNU/Linux) iEYEARECAAYFAkzi1boACgkQ5vihyNWuA4Xq9ACggT4DQr/HzkdDTFt0na0yGLqW TLEAoKtyB8VZy61d2Ug0X8eatHjoFDsu =JMjU -----END PGP SIGNATURE-----

-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 On Tuesday 16 November 2010, Frank Mori Hess wrote:
It looks like it might be a bug in make_shared's perfect forwarding when compiling in c++0x mode.
Another bit of info: getting rid of the static_cast in boost::detail::sp_forward makes both the compiler warning and valgrind error go away, although I have no idea why. -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.9 (GNU/Linux) iEYEARECAAYFAkzi5TwACgkQ5vihyNWuA4UcUgCaAzrGpRsNwELdj6AxoOWQszVp KrwAoItTgtPc0pqyPnY+Ztq3oC/QG6GY =3/MC -----END PGP SIGNATURE-----

Frank Mori Hess wrote:
Another bit of info: getting rid of the static_cast in boost::detail::sp_forward makes both the compiler warning and valgrind error go away, although I have no idea why.
Interesting... which version of GCC? Without the static_cast, it shouldn't compile, as rvalue references no longer bind to lvalues. In fact, I see that I added the static_cast in [61574] for that very reason.

-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 On Tuesday 16 November 2010, Peter Dimov wrote:
Interesting... which version of GCC? Without the static_cast, it shouldn't compile, as rvalue references no longer bind to lvalues. In fact, I see that I added the static_cast in [61574] for that very reason.
$ gcc --version gcc (Debian 4.3.2-1.1) 4.3.2 Its implementation of std::forward looks like: template<typename _Tp> inline _Tp&& forward(typename std::identity<_Tp>::type&& __t) { return __t; } -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.9 (GNU/Linux) iEYEARECAAYFAkzj51EACgkQ5vihyNWuA4WP8QCg3kIa/TqBzQl4JDGnbUKkeAKT Z9UAoIeNJmodVJu6V8JxH/WhnEMi5rf4 =b8yU -----END PGP SIGNATURE-----

Frank Mori Hess wrote:
On Tuesday 16 November 2010, Peter Dimov wrote:
Interesting... which version of GCC? Without the static_cast, it shouldn't compile, as rvalue references no longer bind to lvalues. In fact, I see that I added the static_cast in [61574] for that very reason.
$ gcc --version gcc (Debian 4.3.2-1.1) 4.3.2
Its implementation of std::forward looks like:
template<typename _Tp> inline _Tp&& forward(typename std::identity<_Tp>::type&& __t) { return __t; }
So it appears that we need to revert to the old forward for 4.3 and earlier. The easiest would be to just use std::forward, but unfortunately, I remember that some earlier versions of the standard library didn't have it. :-/
participants (3)
-
Dave Steffen
-
Frank Mori Hess
-
Peter Dimov