Search code examples
c++cmemory-managementmallocfuse

malloc'd memory overlaps object created by std::make_shared


A call to malloc is returning a pointer to a block of memory that overlaps memory already allocated by make_shared. I'm building a FUSE client, and the malloc call is in the FUSE library, but I'm not sure that's relevant. I wasn't able to reproduce the error outside my program, and I've got no idea what to do next. Valgrind doesn't find any errors until a pointer in the object managed by the shared_ptr is corrupted and then used.


Solution

  • This bug was the result of creating a shared_ptr with new, then typecasting it to a weak_ptr and deleting it. I have to typecast it because I'm using a C library (FUSE) and passing it a pointer to a weak_ptr, and it provides a uint64_t to store a handle. The library then calls my functions and passes them a struct containing the pointer typecast as a uint64_t.