I am trying to create a vector with all the file entries inside a folder and then return it, but when it deletes automatically after returning I am getting a bus error on the destructor.
This is my function:
auto list_directory(std::string_view path) -> std::vector<std::filesystem::directory_entry> {
std::vector<std::filesystem::directory_entry> files;
for (const auto& file : std::filesystem::directory_iterator(path)) {
files.emplace_back(file);
}
std::ranges::sort(files.begin(), files.end(), [](const auto& a, const auto& b) {
if (a.is_directory() && !b.is_directory()) {
return true;
}
if (!a.is_directory() && b.is_directory()) {
return false;
}
return a.path().filename() < b.path().filename();
});
return files;
}
It crashes on line 396 from unique_ptr.h:
static_assert(__is_invocable<deleter_type&, pointer>::value,
"unique_ptr's deleter must be invocable with a pointer");
auto& __ptr = _M_t._M_ptr();
if (__ptr != nullptr)
get_deleter()(std::move(__ptr)); // <<< it crashes here
For some reason __ptr is holding the value 0x3000000000000000 there.
This is the call stack when the error happens:
std::unique_ptr<std::filesystem::__cxx11::path::_List::_Impl, std::filesystem::__cxx11::path::_List::_Impl_deleter>::~unique_ptr(std::unique_ptr<std::filesystem::__cxx11::path::_List::_Impl, std::filesystem::__cxx11::path::_List::_Impl_deleter> * this) (\usr\include\c++\12.2.0\bits\unique_ptr.h:396)
std::filesystem::__cxx11::path::_List::~_List(std::filesystem::__cxx11::path::_List * this) (\usr\include\c++\12.2.0\bits\fs_path.h:693)
std::filesystem::__cxx11::path::~path(std::filesystem::__cxx11::path * this) (\usr\include\c++\12.2.0\bits\fs_path.h:354)
std::filesystem::__cxx11::directory_entry::~directory_entry(std::filesystem::__cxx11::directory_entry * this) (\usr\include\c++\12.2.0\bits\fs_dir.h:117)
std::destroy_at<std::filesystem::__cxx11::directory_entry>(std::filesystem::__cxx11::directory_entry * __location) (\usr\include\c++\12.2.0\bits\stl_construct.h:88)
std::_Destroy<std::filesystem::__cxx11::directory_entry>(std::filesystem::__cxx11::directory_entry * __pointer) (\usr\include\c++\12.2.0\bits\stl_construct.h:149)
std::_Destroy_aux<false>::__destroy<std::filesystem::__cxx11::directory_entry*>(std::filesystem::__cxx11::directory_entry * __first, std::filesystem::__cxx11::directory_entry * __last) (\usr\include\c++\12.2.0\bits\stl_construct.h:163)
std::_Destroy<std::filesystem::__cxx11::directory_entry*>(std::filesystem::__cxx11::directory_entry * __first, std::filesystem::__cxx11::directory_entry * __last) (\usr\include\c++\12.2.0\bits\stl_construct.h:193)
std::_Destroy<std::filesystem::__cxx11::directory_entry*, std::filesystem::__cxx11::directory_entry>(std::filesystem::__cxx11::directory_entry * __first, std::filesystem::__cxx11::directory_entry * __last) (\usr\include\c++\12.2.0\bits\alloc_traits.h:850)
std::vector<std::filesystem::__cxx11::directory_entry, std::allocator<std::filesystem::__cxx11::directory_entry> >::~vector(std::vector<std::filesystem::__cxx11::directory_entry, std::allocator<std::filesystem::__cxx11::directory_entry> > * this) (\usr\include\c++\12.2.0\bits\stl_vector.h:730)
ls(std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > args) (\home\gabriel\git\redes\src\functions.cpp:60)
main() (\home\gabriel\git\redes\src\client.cpp:37)
I thought I might be breaking the vector, but I get the same error without the sort and apart from that I don't do anything to it before the crash so I have no idea on what could be causing that error.