I know Segmentation Fault is a general error and could be caused by different wrong memory access scenarios. I tried, but couldn't figure out what's wrong with my code.
This is the minimum reproducible example:
#include <filesystem>#include <iostream>using namespace std;int main(){ filesystem::path filePath("file"); if (filesystem::exists(filePath)) cout << "file exists" << endl; else cout << "file not found" << endl; return 0;}
And this is build command and execution result (the target file doesn't exist).
$ g++ -std=c++17 main.cpp$ lsa.out main.cpp$ ./a.outfile not foundSegmentation fault (core dumped)$ g++ --versiong++ (Ubuntu 8.4.0-3ubuntu2) 8.4.0...
Debugging shows me that the segmentation fault happens on filePath
destruction point, in this part of the header (/usr/include/c++/8/bits/stl_vector.h
):
/** * The dtor only erases the elements, and note that if the * elements themselves are pointers, the pointed-to memory is * not touched in any way. Managing the pointer is the user's * responsibility. */ ~vector() _GLIBCXX_NOEXCEPT { std::_Destroy(this->_M_impl._M_start, this->_M_impl._M_finish, _M_get_Tp_allocator()); _GLIBCXX_ASAN_ANNOTATE_BEFORE_DEALLOC; }
What's wrong in the code or the build command?