How can Valgrind say this:
==13233== Thread 4:
==13233== Invalid write of size 4
==13233== at 0x13AC1F: Server::listener_thread() (realsense_multicam.cpp:136)
==13233== by 0x4E456DA: start_thread (pthread_create.c:463)
==13233== by 0x98F471E: clone (clone.S:95)
==13233== Address 0x1377d938 is 1,192 bytes inside a block of size 1,256 free'd
==13233== at 0x4C3323B: operator delete(void*) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==13233== by 0x14A96F: __gnu_cxx::new_allocator<Server>::deallocate(Server*, unsigned long) (new_allocator.h:125)
==13233== by 0x149489: std::allocator_traits<std::allocator<Server> >::deallocate(std::allocator<Server>&, Server*, unsigned long) (alloc_traits.h:462)
==13233== by 0x1476AB: std::_Vector_base<Server, std::allocator<Server> >::_M_deallocate(Server*, unsigned long) (stl_vector.h:180)
==13233== by 0x145B3B: void std::vector<Server, std::allocator<Server> >::_M_realloc_insert<Server&>(__gnu_cxx::__normal_iterator<Server*, std::vector<Server, std::allocator<Server> > >, Server&) (vector.tcc:448)
==13233== by 0x143974: void std::vector<Server, std::allocator<Server> >::emplace_back<Server&>(Server&) (vector.tcc:105)
==13233== by 0x13B514: main (realsense_multicam.cpp:272)
==13233== Block was alloc'd at
==13233== at 0x4C3217F: operator new(unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==13233== by 0x14A9DE: __gnu_cxx::new_allocator<Server>::allocate(unsigned long, void const*) (new_allocator.h:111)
==13233== by 0x14951C: std::allocator_traits<std::allocator<Server> >::allocate(std::allocator<Server>&, unsigned long) (alloc_traits.h:436)
==13233== by 0x1478FB: std::_Vector_base<Server, std::allocator<Server> >::_M_allocate(unsigned long) (stl_vector.h:172)
==13233== by 0x145A05: void std::vector<Server, std::allocator<Server> >::_M_realloc_insert<Server&>(__gnu_cxx::__normal_iterator<Server*, std::vector<Server, std::allocator<Server> > >, Server&) (vector.tcc:406)
==13233== by 0x143974: void std::vector<Server, std::allocator<Server> >::emplace_back<Server&>(Server&) (vector.tcc:105)
==13233== by 0x13B514: main (realsense_multicam.cpp:272)
sizeof(serv_storage) is not writing anything right?
And then, on line 137, it says:
==13167== Invalid read of size 4
==13167== at 0x13AC43: Server::listener_thread() (realsense_multicam.cpp:137)
==13167== by 0x4E456DA: start_thread (pthread_create.c:463)
==13167== by 0x98F471E: clone (clone.S:95)
==13167== Address 0x1378b828 is 1,256 bytes inside a block of size 2,512 free'd
==13167== at 0x4C3323B: operator delete(void*) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==13167== by 0x14A96F: __gnu_cxx::new_allocator<Server>::deallocate(Server*, unsigned long) (new_allocator.h:125)
==13167== by 0x149489: std::allocator_traits<std::allocator<Server> >::deallocate(std::allocator<Server>&, Server*, unsigned long) (alloc_traits.h:462)
==13167== by 0x1476AB: std::_Vector_base<Server, std::allocator<Server> >::_M_deallocate(Server*, unsigned long) (stl_vector.h:180)
==13167== by 0x145B3B: void std::vector<Server, std::allocator<Server> >::_M_realloc_insert<Server&>(__gnu_cxx::__normal_iterator<Server*, std::vector<Server, std::allocator<Server> > >, Server&) (vector.tcc:448)
==13167== by 0x143974: void std::vector<Server, std::allocator<Server> >::emplace_back<Server&>(Server&) (vector.tcc:105)
==13167== by 0x13B514: main (realsense_multicam.cpp:274)
==13167== Block was alloc'd at
==13167== at 0x4C3217F: operator new(unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==13167== by 0x14A9DE: __gnu_cxx::new_allocator<Server>::allocate(unsigned long, void const*) (new_allocator.h:111)
==13167== by 0x14951C: std::allocator_traits<std::allocator<Server> >::allocate(std::allocator<Server>&, unsigned long) (alloc_traits.h:436)
==13167== by 0x1478FB: std::_Vector_base<Server, std::allocator<Server> >::_M_allocate(unsigned long) (stl_vector.h:172)
==13167== by 0x145A05: void std::vector<Server, std::allocator<Server> >::_M_realloc_insert<Server&>(__gnu_cxx::__normal_iterator<Server*, std::vector<Server, std::allocator<Server> > >, Server&) (vector.tcc:406)
==13167== by 0x143974: void std::vector<Server, std::allocator<Server> >::emplace_back<Server&>(Server&) (vector.tcc:105)
==13167== by 0x13B514: main (realsense_multicam.cpp:274)
the relevant code in Server:
class Server {
public:
Server(int port);
void * listener_thread();
void init_listener_thread();
void update_buffer(const unsigned char * data, int offset, unsigned long numbytes);
private:
int init_sock, conn_sock;
char * send_buffer;
int buffer_size = 1024;
char receive_buffer[1024];
struct sockaddr_in serv_addr;
struct sockaddr_storage serv_storage;
socklen_t addr_size;
pthread_mutex_t buffer_access_mutex;
pthread_t listener_thread_id;
unsigned long frame_size;
};
CodePudding user response:
The valgrind output tells us that while calling emplace_back
on a std::vector<Server>
, the vector ran out of capacity and had to _M_realloc_insert
, that is, reallocate memory and move all its objects over to the new buffer.
I suspect you are holding a reference/pointer to a Server
that lives inside your vector, which was invalidated when the vector deleted the old capacity buffer.
You should be careful with references/pointer or iterators to vectors that you intend to extend. Hold an index instead.
CodePudding user response:
Best guess:
Your code is deleting the instance of Server before ending the thread running inside Server::listener_thread