I'm trying to emulate a game server but, when I try to run my LoginServer asynchronously I'm given the "Conditional jump or move depends on uninitialized value(s)" error through Valgrind. However, if I don't try to run it asynchronously and just run the server from the main thread I don't receive the error. Is Valgrind throwing a false positive or am I just going crazy?
I've provided a simplified example for reference
#include <future>
#include <iostream>
class A
{
public:
A()
{
this->i = 0;
}
A(const A &a2)
{
this->i = a2.i;
}
A(A&& a)
{
this->i = std::move(a.i);
}
~A()
{
this->i = 0;
}
bool setI(int i)
{
this->i = i;
return true;
}
int getI()
{
return i;
}
A& operator=(const A& a)
{
this->i = a.i;
return *this;
}
private:
int i;
};
int main()
{
A a = A();
std::async(std::launch::async, &A::setI, &a, 2);
std::cout << a.getI() << '\n';
std::async(std::launch::async, &A::setI, &a, 3);
std::cout << a.getI() << '\n';
std::async(std::launch::async, &A::setI, &a, 4);
std::cout << a.getI() << '\n';
std::async(std::launch::async, &A::setI, &a, 5);
std::cout << a.getI() << '\n';
return 0;
}
Valgrind report
==10108== Memcheck, a memory error detector
==10108== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==10108== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
==10108== Command: ./test
==10108==
==10108== Conditional jump or move depends on uninitialised value(s)
==10108== at 0xFFFF0FCC: ???
==10108== Uninitialised value was created by a heap allocation
==10108== at 0x4847D4C: operator new(unsigned int) (vg_replace_malloc.c:328)
==10108== by 0x160E3: __gnu_cxx::new_allocator<std::_Sp_counted_ptr_inplace<std::__future_base::_Async_state_impl<std::_Bind_simple<std::_Mem_fn<bool (A::*)(int)> (A*, int)>, bool>, std::allocator<std::__future_base::_Async_state_impl<std::_Bind_simple<std::_Mem_fn<bool (A::*)(int)> (A*, int)>, bool> >, (__gnu_cxx::_Lock_policy)1> >::allocate(unsigned int, void const*) (new_allocator.h:104)
==10108== by 0x15C77: std::allocator_traits<std::allocator<std::_Sp_counted_ptr_inplace<std::__future_base::_Async_state_impl<std::_Bind_simple<std::_Mem_fn<bool (A::*)(int)> (A*, int)>, bool>, std::allocator<std::__future_base::_Async_state_impl<std::_Bind_simple<std::_Mem_fn<bool (A::*)(int)> (A*, int)>, bool> >, (__gnu_cxx::_Lock_policy)1> > >::allocate(std::allocator<std::_Sp_counted_ptr_inplace<std::__future_base::_Async_state_impl<std::_Bind_simple<std::_Mem_fn<bool (A::*)(int)> (A*, int)>, bool>, std::allocator<std::__future_base::_Async_state_impl<std::_Bind_simple<std::_Mem_fn<bool (A::*)(int)> (A*, int)>, bool> >, (__gnu_cxx::_Lock_policy)1> >&, unsigned int) (alloc_traits.h:436)
==10108== by 0x15767: std::__allocated_ptr<std::allocator<std::_Sp_counted_ptr_inplace<std::__future_base::_Async_state_impl<std::_Bind_simple<std::_Mem_fn<bool (A::*)(int)> (A*, int)>, bool>, std::allocator<std::__future_base::_Async_state_impl<std::_Bind_simple<std::_Mem_fn<bool (A::*)(int)> (A*, int)>, bool> >, (__gnu_cxx::_Lock_policy)1> > > std::__allocate_guarded<std::allocator<std::_Sp_counted_ptr_inplace<std::__future_base::_Async_state_impl<std::_Bind_simple<std::_Mem_fn<bool (A::*)(int)> (A*, int)>, bool>, std::allocator<std::__future_base::_Async_state_impl<std::_Bind_simple<std::_Mem_fn<bool (A::*)(int)> (A*, int)>, bool> >, (__gnu_cxx::_Lock_policy)1> > >(std::allocator<std::_Sp_counted_ptr_inplace<std::__future_base::_Async_state_impl<std::_Bind_simple<std::_Mem_fn<bool (A::*)(int)> (A*, int)>, bool>, std::allocator<std::__future_base::_Async_state_impl<std::_Bind_simple<std::_Mem_fn<bool (A::*)(int)> (A*, int)>, bool> >, (__gnu_cxx::_Lock_policy)1> >&) (allocated_ptr.h:103)
==10108== by 0x1548B: std::__shared_count<(__gnu_cxx::_Lock_policy)1>::__shared_count<std::__future_base::_Async_state_impl<std::_Bind_simple<std::_Mem_fn<bool (A::*)(int)> (A*, int)>, bool>, std::allocator<std::__future_base::_Async_state_impl<std::_Bind_simple<std::_Mem_fn<bool (A::*)(int)> (A*, int)>, bool> >, std::_Bind_simple<std::_Mem_fn<bool (A::*)(int)> (A*, int)> >(std::_Sp_make_shared_tag, std::__future_base::_Async_state_impl<std::_Bind_simple<std::_Mem_fn<bool (A::*)(int)> (A*, int)>, bool>*, std::allocator<std::__future_base::_Async_state_impl<std::_Bind_simple<std::_Mem_fn<bool (A::*)(int)> (A*, int)>, bool> > const&, std::_Bind_simple<std::_Mem_fn<bool (A::*)(int)> (A*, int)>&&) (shared_ptr_base.h:613)
==10108== by 0x15293: std::__shared_ptr<std::__future_base::_Async_state_impl<std::_Bind_simple<std::_Mem_fn<bool (A::*)(int)> (A*, int)>, bool>, (__gnu_cxx::_Lock_policy)1>::__shared_ptr<std::allocator<std::__future_base::_Async_state_impl<std::_Bind_simple<std::_Mem_fn<bool (A::*)(int)> (A*, int)>, bool> >, std::_Bind_simple<std::_Mem_fn<bool (A::*)(int)> (A*, int)> >(std::_Sp_make_shared_tag, std::allocator<std::__future_base::_Async_state_impl<std::_Bind_simple<std::_Mem_fn<bool (A::*)(int)> (A*, int)>, bool> > const&, std::_Bind_simple<std::_Mem_fn<bool (A::*)(int)> (A*, int)>&&) (shared_ptr_base.h:1100)
==10108== by 0x1513B: std::shared_ptr<std::__future_base::_Async_state_impl<std::_Bind_simple<std::_Mem_fn<bool (A::*)(int)> (A*, int)>, bool> >::shared_ptr<std::allocator<std::__future_base::_Async_state_impl<std::_Bind_simple<std::_Mem_fn<bool (A::*)(int)> (A*, int)>, bool> >, std::_Bind_simple<std::_Mem_fn<bool (A::*)(int)> (A*, int)> >(std::_Sp_make_shared_tag, std::allocator<std::__future_base::_Async_state_impl<std::_Bind_simple<std::_Mem_fn<bool (A::*)(int)> (A*, int)>, bool> > const&, std::_Bind_simple<std::_Mem_fn<bool (A::*)(int)> (A*, int)>&&) (shared_ptr.h:319)
==10108== by 0x14DEF: std::shared_ptr<std::__future_base::_Async_state_impl<std::_Bind_simple<std::_Mem_fn<bool (A::*)(int)> (A*, int)>, bool> > std::allocate_shared<std::__future_base::_Async_state_impl<std::_Bind_simple<std::_Mem_fn<bool (A::*)(int)> (A*, int)>, bool>, std::allocator<std::__future_base::_Async_state_impl<std::_Bind_simple<std::_Mem_fn<bool (A::*)(int)> (A*, int)>, bool> >, std::_Bind_simple<std::_Mem_fn<bool (A::*)(int)> (A*, int)> >(std::allocator<std::__future_base::_Async_state_impl<std::_Bind_simple<std::_Mem_fn<bool (A::*)(int)> (A*, int)>, bool> > const&, std::_Bind_simple<std::_Mem_fn<bool (A::*)(int)> (A*, int)>&&) (shared_ptr.h:620)
==10108== by 0x14A57: std::shared_ptr<std::__future_base::_Async_state_impl<std::_Bind_simple<std::_Mem_fn<bool (A::*)(int)> (A*, int)>, bool> > std::make_shared<std::__future_base::_Async_state_impl<std::_Bind_simple<std::_Mem_fn<bool (A::*)(int)> (A*, int)>, bool>, std::_Bind_simple<std::_Mem_fn<bool (A::*)(int)> (A*, int)> >(std::_Bind_simple<std::_Mem_fn<bool (A::*)(int)> (A*, int)>&&) (shared_ptr.h:636)
==10108== by 0x1446B: std::shared_ptr<std::__future_base::_State_baseV2> std::__future_base::_S_make_async_state<std::_Bind_simple<std::_Mem_fn<bool (A::*)(int)> (A*, int)> >(std::_Bind_simple<std::_Mem_fn<bool (A::*)(int)> (A*, int)>&&) (future:1702)
==10108== by 0x137FF: std::future<std::result_of<std::decay<bool (A::*)(int)>::type (std::decay<A*>::type, std::decay<int>::type)>::type> std::async<bool (A::*)(int), A*, int>(std::launch, bool (A::*&&)(int), A*&&, int&&) (future:1716)
==10108== by 0x118EF: main (main.cpp:47)
==10108==
==10108== Conditional jump or move depends on uninitialised value(s)
==10108== at 0xFFFF0FD4: ???
==10108== Uninitialised value was created by a heap allocation
==10108== at 0x4847D4C: operator new(unsigned int) (vg_replace_malloc.c:328)
==10108== by 0x160E3: __gnu_cxx::new_allocator<std::_Sp_counted_ptr_inplace<std::__future_base::_Async_state_impl<std::_Bind_simple<std::_Mem_fn<bool (A::*)(int)> (A*, int)>, bool>, std::allocator<std::__future_base::_Async_state_impl<std::_Bind_simple<std::_Mem_fn<bool (A::*)(int)> (A*, int)>, bool> >, (__gnu_cxx::_Lock_policy)1> >::allocate(unsigned int, void const*) (new_allocator.h:104)
==10108== by 0x15C77: std::allocator_traits<std::allocator<std::_Sp_counted_ptr_inplace<std::__future_base::_Async_state_impl<std::_Bind_simple<std::_Mem_fn<bool (A::*)(int)> (A*, int)>, bool>, std::allocator<std::__future_base::_Async_state_impl<std::_Bind_simple<std::_Mem_fn<bool (A::*)(int)> (A*, int)>, bool> >, (__gnu_cxx::_Lock_policy)1> > >::allocate(std::allocator<std::_Sp_counted_ptr_inplace<std::__future_base::_Async_state_impl<std::_Bind_simple<std::_Mem_fn<bool (A::*)(int)> (A*, int)>, bool>, std::allocator<std::__future_base::_Async_state_impl<std::_Bind_simple<std::_Mem_fn<bool (A::*)(int)> (A*, int)>, bool> >, (__gnu_cxx::_Lock_policy)1> >&, unsigned int) (alloc_traits.h:436)
==10108== by 0x15767: std::__allocated_ptr<std::allocator<std::_Sp_counted_ptr_inplace<std::__future_base::_Async_state_impl<std::_Bind_simple<std::_Mem_fn<bool (A::*)(int)> (A*, int)>, bool>, std::allocator<std::__future_base::_Async_state_impl<std::_Bind_simple<std::_Mem_fn<bool (A::*)(int)> (A*, int)>, bool> >, (__gnu_cxx::_Lock_policy)1> > > std::__allocate_guarded<std::allocator<std::_Sp_counted_ptr_inplace<std::__future_base::_Async_state_impl<std::_Bind_simple<std::_Mem_fn<bool (A::*)(int)> (A*, int)>, bool>, std::allocator<std::__future_base::_Async_state_impl<std::_Bind_simple<std::_Mem_fn<bool (A::*)(int)> (A*, int)>, bool> >, (__gnu_cxx::_Lock_policy)1> > >(std::allocator<std::_Sp_counted_ptr_inplace<std::__future_base::_Async_state_impl<std::_Bind_simple<std::_Mem_fn<bool (A::*)(int)> (A*, int)>, bool>, std::allocator<std::__future_base::_Async_state_impl<std::_Bind_simple<std::_Mem_fn<bool (A::*)(int)> (A*, int)>, bool> >, (__gnu_cxx::_Lock_policy)1> >&) (allocated_ptr.h:103)
==10108== by 0x1548B: std::__shared_count<(__gnu_cxx::_Lock_policy)1>::__shared_count<std::__future_base::_Async_state_impl<std::_Bind_simple<std::_Mem_fn<bool (A::*)(int)> (A*, int)>, bool>, std::allocator<std::__future_base::_Async_state_impl<std::_Bind_simple<std::_Mem_fn<bool (A::*)(int)> (A*, int)>, bool> >, std::_Bind_simple<std::_Mem_fn<bool (A::*)(int)> (A*, int)> >(std::_Sp_make_shared_tag, std::__future_base::_Async_state_impl<std::_Bind_simple<std::_Mem_fn<bool (A::*)(int)> (A*, int)>, bool>*, std::allocator<std::__future_base::_Async_state_impl<std::_Bind_simple<std::_Mem_fn<bool (A::*)(int)> (A*, int)>, bool> > const&, std::_Bind_simple<std::_Mem_fn<bool (A::*)(int)> (A*, int)>&&) (shared_ptr_base.h:613)
==10108== by 0x15293: std::__shared_ptr<std::__future_base::_Async_state_impl<std::_Bind_simple<std::_Mem_fn<bool (A::*)(int)> (A*, int)>, bool>, (__gnu_cxx::_Lock_policy)1>::__shared_ptr<std::allocator<std::__future_base::_Async_state_impl<std::_Bind_simple<std::_Mem_fn<bool (A::*)(int)> (A*, int)>, bool> >, std::_Bind_simple<std::_Mem_fn<bool (A::*)(int)> (A*, int)> >(std::_Sp_make_shared_tag, std::allocator<std::__future_base::_Async_state_impl<std::_Bind_simple<std::_Mem_fn<bool (A::*)(int)> (A*, int)>, bool> > const&, std::_Bind_simple<std::_Mem_fn<bool (A::*)(int)> (A*, int)>&&) (shared_ptr_base.h:1100)
==10108== by 0x1513B: std::shared_ptr<std::__future_base::_Async_state_impl<std::_Bind_simple<std::_Mem_fn<bool (A::*)(int)> (A*, int)>, bool> >::shared_ptr<std::allocator<std::__future_base::_Async_state_impl<std::_Bind_simple<std::_Mem_fn<bool (A::*)(int)> (A*, int)>, bool> >, std::_Bind_simple<std::_Mem_fn<bool (A::*)(int)> (A*, int)> >(std::_Sp_make_shared_tag, std::allocator<std::__future_base::_Async_state_impl<std::_Bind_simple<std::_Mem_fn<bool (A::*)(int)> (A*, int)>, bool> > const&, std::_Bind_simple<std::_Mem_fn<bool (A::*)(int)> (A*, int)>&&) (shared_ptr.h:319)
==10108== by 0x14DEF: std::shared_ptr<std::__future_base::_Async_state_impl<std::_Bind_simple<std::_Mem_fn<bool (A::*)(int)> (A*, int)>, bool> > std::allocate_shared<std::__future_base::_Async_state_impl<std::_Bind_simple<std::_Mem_fn<bool (A::*)(int)> (A*, int)>, bool>, std::allocator<std::__future_base::_Async_state_impl<std::_Bind_simple<std::_Mem_fn<bool (A::*)(int)> (A*, int)>, bool> >, std::_Bind_simple<std::_Mem_fn<bool (A::*)(int)> (A*, int)> >(std::allocator<std::__future_base::_Async_state_impl<std::_Bind_simple<std::_Mem_fn<bool (A::*)(int)> (A*, int)>, bool> > const&, std::_Bind_simple<std::_Mem_fn<bool (A::*)(int)> (A*, int)>&&) (shared_ptr.h:620)
==10108== by 0x14A57: std::shared_ptr<std::__future_base::_Async_state_impl<std::_Bind_simple<std::_Mem_fn<bool (A::*)(int)> (A*, int)>, bool> > std::make_shared<std::__future_base::_Async_state_impl<std::_Bind_simple<std::_Mem_fn<bool (A::*)(int)> (A*, int)>, bool>, std::_Bind_simple<std::_Mem_fn<bool (A::*)(int)> (A*, int)> >(std::_Bind_simple<std::_Mem_fn<bool (A::*)(int)> (A*, int)>&&) (shared_ptr.h:636)
==10108== by 0x1446B: std::shared_ptr<std::__future_base::_State_baseV2> std::__future_base::_S_make_async_state<std::_Bind_simple<std::_Mem_fn<bool (A::*)(int)> (A*, int)> >(std::_Bind_simple<std::_Mem_fn<bool (A::*)(int)> (A*, int)>&&) (future:1702)
==10108== by 0x137FF: std::future<std::result_of<std::decay<bool (A::*)(int)>::type (std::decay<A*>::type, std::decay<int>::type)>::type> std::async<bool (A::*)(int), A*, int>(std::launch, bool (A::*&&)(int), A*&&, int&&) (future:1716)
==10108== by 0x118EF: main (main.cpp:47)
==10108==
2
3
4
5
==10108==
==10108== HEAP SUMMARY:
==10108== in use at exit: 0 bytes in 0 blocks
==10108== total heap usage: 15 allocs, 15 frees, 21,808 bytes allocated
==10108==
==10108== All heap blocks were freed -- no leaks are possible
==10108==
==10108== For counts of detected and suppressed errors, rerun with: -v
==10108== ERROR SUMMARY: 12 errors from 2 contexts (suppressed: 6 from 3)
Clearly, a
and its member is initialized, why am I receiving this report?
Is my compiler or valgrind build throwing this for no reason or am I just missing something?
Anyone have any ideas? Thanks in advance for any help.
My makefile:
CC = g++
CFLAGS = -Wall -g -std=c++14 -pthread
SOURCES = main.cpp
all :
$(CC) $(CFLAGS) $(SOURCES) -o test
clean :
rm -f test
Compiled with: g++ (Raspbian 6.3.0-18+rpi1+deb9u1) 6.3.0 20170516 on Raspbian Stretch
Valgrind version 3.13.0