Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do I get uninitialized-value warnings from Valgrind when I use Boost UUID?

I have a class with members:

std::string mName;
boost::uuids::uuid mId;

In the constructor I wrote:

mName = boost::lexical_cast<std::string>(mId);

Valgrind writes:

==30714== Use of uninitialised value of size 8
==30714==    at 0x69E92FC: ??? (in /usr/lib/libstdc++.so.6.0.16)
==30714==    by 0x69E93EE: std::ostreambuf_iterator<char, std::char_traits<char> > std::num_put<char, std::ostreambuf_iterator<char, std::char_traits<char> > >::_M_insert_int<unsigned long>(std::ostreambuf_iterator<char, std::char_traits<char> >, std::ios_base&, char, unsigned long) const (in /usr/lib/libstdc++.so.6.0.16)
==30714==    by 0x69E9705: std::num_put<char, std::ostreambuf_iterator<char, std::char_traits<char> > >::do_put(std::ostreambuf_iterator<char, std::char_traits<char> >, std::ios_base&, char, unsigned long) const (in /usr/lib/libstdc++.so.6.0.16)
==30714==    by 0x69F522C: std::ostream& std::ostream::_M_insert<unsigned long>(unsigned long) (in /usr/lib/libstdc++.so.6.0.16)
==30714==    by 0x6283A5: std::basic_ostream<char, std::char_traits<char> >& boost::uuids::operator<< <char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, boost::uuids::uuid const&) (uuid_io.hpp:55)
==30714==    by 0x62799D: bool boost::detail::lexical_stream_limited_src<char, std::char_traits<char>, true>::shl_input_streamable<boost::uuids::uuid const>(boost::uuids::uuid const&) (lexical_cast.hpp:1260)
==30714==    by 0x626DB4: bool boost::detail::lexical_stream_limited_src<char, std::char_traits<char>, true>::operator<< <boost::uuids::uuid>(boost::uuids::uuid const&) (lexical_cast.hpp:1431)
==30714==    by 0x6261EE: boost::detail::lexical_cast_do_cast<std::string, boost::uuids::uuid>::lexical_cast_impl(boost::uuids::uuid const&) (lexical_cast.hpp:1920)
==30714==    by 0x62590C: std::string boost::lexical_cast<std::string, boost::uuids::uuid>(boost::uuids::uuid const&) (lexical_cast.hpp:2087)
==30714==    by 0x624145: th::Object::Object(std::string const&) (Object.cpp:25)
t==30714== Conditional jump or move depends on uninitialised value(s)

What could it be?

like image 925
Max Frai Avatar asked Apr 03 '12 20:04

Max Frai


People also ask

What does conditional jump mean in Valgrind?

Uninitialized value errors The error message "Conditional jump or move depends on uninitialized value(s)" essentially means Valgrind has determined that the result of your program depends on uninitialized memory. Sometimes you will also see the message "Use of uninitialized value of size N".

How do you fix the conditional jump in Valgrind?

Conditional jump or move depends on uninitialized value(s) This error is caused if you forget to initialize variables before using or accessing them. You can usually re-run valgrind with the flag --track-origins=yes to see where the uninitialized value came from.


1 Answers

boost::uuids::uuid is a POD type, so default-initializing a uuid object will in fact leave it uninitialized.

Make sure you value-initialize your uuid object inside of your constructor's initialization list:

class MyType
{
    std::string mName;
    boost::uuids::uuid mId;

public:
    MyType()
      : mName(), // not technically necessary since std::string has a
                 // non-trivial default constructor, but good for consistency
        mId() // necessary in order to actually initialize mId, remains
              // uninitialized otherwise
    {
        mName = boost::lexical_cast<std::string>(mId);
    }
};

Or better yet, by reordering your data member declarations:

class MyType
{
    boost::uuids::uuid mId;
    std::string mName;

public:
    MyType()
      : mId(), // value-initialize mId
        mName(boost::lexical_cast<std::string>(mId)) // avoid unnecessary default
                                                     // construction of mName
    { }
};
like image 139
ildjarn Avatar answered Sep 22 '22 08:09

ildjarn