I face a problem in my program that I can't get over it. I have a class in which I have declared an attribute like std::filesystem::path p {"path/to/save/dir"};
. And I have in the same class a method which saves some data to a .txt file. First I check some things like if the user has given an extension and then if a file with the same name exists. So if the user does not want to overwrite it, I ask him for a new name file. Then I write to the file. So, the problem is that whenever the program exits the class a seg fault will pop and specifically for the save method if it goes into the if clause then it will not save as it will recieve seg fault. I think that it has to do with the destructor of the class and calling the destructor of the path object.
Using gdb, I got this as the message:
0x0000555555558907 in std::vector<std::filesystem::__cxx11::path::_Cmpt, std::allocator<std::filesystem::__cxx11::path::_Cmpt> >::~vector (this=0x20,
__in_chrg=<optimized out>) at /usr/include/c++/8/bits/stl_vector.h:567
567 std::_Destroy(this->_M_impl._M_start, this->_M_impl._M_finish,
Things I have tried so far:
- I have tried explicitly calling the
std::filesystem::path
destructor (my class does not have a destructor method specified right now). Did not work. - I have tried moving the path object outside the class, "converting" it to a global object. The problem would now occur only when I would exit from the main.cpp file where I declare the class object*****. But not when I would exit from the class.
- I have tried to clear the path with the clear method but it would not be called for some reason that I never understood. So it did not do anything.
*:
case 1: {
Game g1(true);
while(g1.play()){}
break;
}
Essentially, this is a part of the main.cpp. It has a menu followed by a switch statement analogous with the selection of the user. So when it exists from the loop and breaks and I choose to exit I recieve the seg fault.
Any help is appriciated, thanks.