directory_iterator 遇到段错误
directory_iterator runs into segfault
这是我的代码:
#include <iostream>
#include <filesystem>
int main(int argc, char *argv[]) {
auto iter = std::filesystem::directory_iterator("foo");
for (auto &entry : iter) {
std::cout << entry.path();
}
}
当我 运行 它和目录 foo
存在时,我得到一个 SIGSEGV。所以我启动了 gdb:
(gdb) run
Starting program: /home/krausefx/a.out
Program received signal SIGSEGV, Segmentation fault.
0x0000555555556a87 in std::vector<std::filesystem::__cxx11::path::_Cmpt, std::allocator<std::filesystem::__cxx11::path::_Cmpt> >::~vector (
this=0x23) at /usr/include/c++/8/bits/stl_vector.h:567
567 std::_Destroy(this->_M_impl._M_start, this->_M_impl._M_finish,
(gdb) backtrace
#0 0x0000555555556a87 in std::vector<std::filesystem::__cxx11::path::_Cmpt, std::allocator<std::filesystem::__cxx11::path::_Cmpt> >::~vector (
this=0x23) at /usr/include/c++/8/bits/stl_vector.h:567
#1 0x00005555555566aa in std::filesystem::__cxx11::path::~path (this=0x3) at /usr/include/c++/8/bits/fs_path.h:208
#2 0x0000555555557ebe in std::filesystem::__cxx11::path::_Cmpt::~_Cmpt (this=<incomplete type>) at /usr/include/c++/8/bits/fs_path.h:643
#3 0x0000555555557ed9 in std::_Destroy<std::filesystem::__cxx11::path::_Cmpt> (__pointer=0x3) at /usr/include/c++/8/bits/stl_construct.h:98
#4 0x0000555555557ced in std::_Destroy_aux<false>::__destroy<std::filesystem::__cxx11::path::_Cmpt*> (__first=0x3, __last=0x0)
at /usr/include/c++/8/bits/stl_construct.h:108
#5 0x00005555555576de in std::_Destroy<std::filesystem::__cxx11::path::_Cmpt*> (__first=0x3, __last=0x0)
at /usr/include/c++/8/bits/stl_construct.h:137
#6 0x0000555555556fb9 in std::_Destroy<std::filesystem::__cxx11::path::_Cmpt*, std::filesystem::__cxx11::path::_Cmpt> (__first=0x3, __last=0x0)
at /usr/include/c++/8/bits/stl_construct.h:206
#7 0x0000555555556a9d in std::vector<std::filesystem::__cxx11::path::_Cmpt, std::allocator<std::filesystem::__cxx11::path::_Cmpt> >::~vector (
this=0x7fffffffdcf0) at /usr/include/c++/8/bits/stl_vector.h:567
#8 0x00005555555566aa in std::filesystem::__cxx11::path::~path (this=0x7fffffffdcd0) at /usr/include/c++/8/bits/fs_path.h:208
#9 0x000055555555630d in main (argc=32767, argv=0x7ffff7fadf40 <std::wcout>) at test.cpp:5
(gdb) p this
= (vector * const) 0x23
显然,当初始化 directory_iterator
时,std::filesystem::path
的析构函数由于某种原因被调用,并且在某处,std::vector
的析构函数在 [= 0x23
的 17=] 值,这显然是一件坏事并导致 SIGSEGV。
这里发生了什么?难道我做错了什么?这是编译器错误吗(编译器是 g++ 8.3.0)?
我认为 std::filesystem 不稳定。它在我的项目中引起了段错误和其他问题(尤其是 msys2 附带的 mingw-w64 中的 std::filesystem::path)。尝试更新您的 gcc 包并检查问题是否仍然存在。如果确实如此,那么您可以提交错误报告,或者只是等待并希望有人已经报告了它(在我的情况下更新解决了问题)。
我检查过 directory_iterator
在 Ubuntu 下使用 GCC 8 工作正常。
一定要在编译时加上链接器标志-lstdc++fs
。
如果您没有成功结束编译,但至少在我的系统中,我会在开始迭代时遇到段错误。
这是我的代码:
#include <iostream>
#include <filesystem>
int main(int argc, char *argv[]) {
auto iter = std::filesystem::directory_iterator("foo");
for (auto &entry : iter) {
std::cout << entry.path();
}
}
当我 运行 它和目录 foo
存在时,我得到一个 SIGSEGV。所以我启动了 gdb:
(gdb) run
Starting program: /home/krausefx/a.out
Program received signal SIGSEGV, Segmentation fault.
0x0000555555556a87 in std::vector<std::filesystem::__cxx11::path::_Cmpt, std::allocator<std::filesystem::__cxx11::path::_Cmpt> >::~vector (
this=0x23) at /usr/include/c++/8/bits/stl_vector.h:567
567 std::_Destroy(this->_M_impl._M_start, this->_M_impl._M_finish,
(gdb) backtrace
#0 0x0000555555556a87 in std::vector<std::filesystem::__cxx11::path::_Cmpt, std::allocator<std::filesystem::__cxx11::path::_Cmpt> >::~vector (
this=0x23) at /usr/include/c++/8/bits/stl_vector.h:567
#1 0x00005555555566aa in std::filesystem::__cxx11::path::~path (this=0x3) at /usr/include/c++/8/bits/fs_path.h:208
#2 0x0000555555557ebe in std::filesystem::__cxx11::path::_Cmpt::~_Cmpt (this=<incomplete type>) at /usr/include/c++/8/bits/fs_path.h:643
#3 0x0000555555557ed9 in std::_Destroy<std::filesystem::__cxx11::path::_Cmpt> (__pointer=0x3) at /usr/include/c++/8/bits/stl_construct.h:98
#4 0x0000555555557ced in std::_Destroy_aux<false>::__destroy<std::filesystem::__cxx11::path::_Cmpt*> (__first=0x3, __last=0x0)
at /usr/include/c++/8/bits/stl_construct.h:108
#5 0x00005555555576de in std::_Destroy<std::filesystem::__cxx11::path::_Cmpt*> (__first=0x3, __last=0x0)
at /usr/include/c++/8/bits/stl_construct.h:137
#6 0x0000555555556fb9 in std::_Destroy<std::filesystem::__cxx11::path::_Cmpt*, std::filesystem::__cxx11::path::_Cmpt> (__first=0x3, __last=0x0)
at /usr/include/c++/8/bits/stl_construct.h:206
#7 0x0000555555556a9d in std::vector<std::filesystem::__cxx11::path::_Cmpt, std::allocator<std::filesystem::__cxx11::path::_Cmpt> >::~vector (
this=0x7fffffffdcf0) at /usr/include/c++/8/bits/stl_vector.h:567
#8 0x00005555555566aa in std::filesystem::__cxx11::path::~path (this=0x7fffffffdcd0) at /usr/include/c++/8/bits/fs_path.h:208
#9 0x000055555555630d in main (argc=32767, argv=0x7ffff7fadf40 <std::wcout>) at test.cpp:5
(gdb) p this
= (vector * const) 0x23
显然,当初始化 directory_iterator
时,std::filesystem::path
的析构函数由于某种原因被调用,并且在某处,std::vector
的析构函数在 [= 0x23
的 17=] 值,这显然是一件坏事并导致 SIGSEGV。
这里发生了什么?难道我做错了什么?这是编译器错误吗(编译器是 g++ 8.3.0)?
我认为 std::filesystem 不稳定。它在我的项目中引起了段错误和其他问题(尤其是 msys2 附带的 mingw-w64 中的 std::filesystem::path)。尝试更新您的 gcc 包并检查问题是否仍然存在。如果确实如此,那么您可以提交错误报告,或者只是等待并希望有人已经报告了它(在我的情况下更新解决了问题)。
我检查过 directory_iterator
在 Ubuntu 下使用 GCC 8 工作正常。
一定要在编译时加上链接器标志-lstdc++fs
。
如果您没有成功结束编译,但至少在我的系统中,我会在开始迭代时遇到段错误。