sizeof 上的写入无效

use*_*180 0 c++ valgrind c++11 realsense

在此输入图像描述

Valgrind 怎么说:

==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)
Run Code Online (Sandbox Code Playgroud)

sizeof(serv_storage) 没有写任何东西吗?

然后,在第 137 行,它说:

==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)
Run Code Online (Sandbox Code Playgroud)

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;
};
Run Code Online (Sandbox Code Playgroud)

bit*_*ask 5

valgrind 输出告诉我们,在调用emplace_backa时std::vector<Server>,向量耗尽了容量_M_realloc_insert,必须重新分配内存并将其所有对象移至新缓冲区。

我怀疑您持有一个指向Server向量内部的引用/指针,当向量删除旧的容量缓冲区时,该引用/指针就失效了。您应该小心对待要扩展的向量的引用/指针或迭代器。相反,保留一个索引。

  • 我想我看到了问题所在。代码[在上面的链接共享](https://gist.github.com/richardrl/aa0fe77c10606cbf873a65c349a07bc7#file-realsense_multicam-cpp-L249)在将“Server”实例存储到向量中时创建了一个副本 - 其中有一个活动线程在上面运行!建议:从堆中创建 `Storage` 实例(即“新建”它)并将其存储为指针向量。 (2认同)
  • 如果您绝对需要使用指针,请使用“std::unique_ptr”和“std::make_unique”或“std::shared_ptr”或“std::make_shared”,而不是原始指针。 (2认同)