1

I have recently started using std::filesystem, and I have faced an issue that I could not fix. I have a class with a function that periodically iterates over some directories and in case new files are found, it adds its name and size to a map container. But I constantly either received segmentation fault or std::bad_alloc. I therefore wrote a simple test to see if it works out of my relatively large and complex make file project. But I still receive segmentation fault.

I simply get the number of files in a directory this way:

int main(){
const fs::path Path = fs::current_path();
for (const auto& p:fs::directory_iterator(Path)){
        count++;
    }
return count;
}

the error is somehow connected to path object destructor. The debugger throws the following error:

Program received signal SIGSEGV, Segmentation fault. 0x0000555555556693 in std::vector<std::filesystem::__cxx11::path::_Cmpt, >std::allocatorstd::filesystem::__cxx11::path::_Cmpt >::~vector (this=0x20, >__in_chrg=) at /usr/include/c++/8/bits/stl_vector.h:567 567 std::_Destroy(this->_M_impl._M_start, this->_M_impl._M_finish,

Program terminated with signal SIGSEGV, Segmentation fault.

Now, I've seen few posts about g++ issue with filesystem, like this one: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=90050

I use g++ 8.4.0 on an Ubuntu 18.4 machine. I tried in my makefile project to add -lstdc++fs to linker flags, but it did not make a difference.

So the question is what is the root cause of this error? Is it really caused by g++? thanks in advance for helping!

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
Ma Ta
  • 69
  • 4
  • Did you see Jonathan Wakely's solution in [this bug](https://bugs.launchpad.net/ubuntu/+source/gcc-8/+bug/1824721/comments/6) you need to ensure you're linking the library last. But C++17 support on that compiler is _experimental_ and is known to have issues. – Mgetz Sep 09 '21 at 18:04
  • I infact added the flag to eclipse linker and it worked! – Ma Ta Sep 09 '21 at 18:05
  • I tried also with my makefile, and it now seems to work. I actually had to put it before -lrt in the libraries:) – Ma Ta Sep 09 '21 at 18:06

0 Answers0