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!
Related
I am trying to use libdxf library. I have created a small program running which I am getting a segmentation fault. I have debugged my code using gdb and have located the code line causing the core to be dumped.
My code snippet is as written below:
#include<iostream>
#include<../src/dl_dxf.h>
#include<../src/dl_creationadapter.h>
using namespace std;
class Test:public DL_CreationAdapter {
public:
void write();
};
void Test::write(){
DL_Dxf *dxf = new DL_Dxf(); // fault here
DL_Codes::version exportVersion = DL_Codes::AC1015;
DL_WriterA* dw = dxf->out("myfile.dxf", exportVersion);
if (dw==NULL) {
printf("Cannot open file 'myfile.dxf' \
for writing.");
}
delete dxf;
}
int main(){
Test test;
test.write(); // fault here
return 0;
}
The source of segmentation fault is in 2 lines above with the comment //fault here.
How to handle this segmentation fault?
I wish you are working on Linux.
I compiled your code an ran succesfully just by replacing
#include<../src/dl_dxf.h>
#include<../src/dl_creationadapter.h>
with
#include<dxflib/dl_dxf.h>
#include<dxflib/dl_creationadapter.h>
I installed libdxflib-dev package via package manager on Ubuntu(And also libdxflib-2.5.0.0 that includes the libdxflib.so).
It created an empty 'myfile.dxf' and exited.
Since you include dxf headers from another directory I suspect that there is a compatibility problem (possibly between dxf headers and the shared object file)
If you are an Ubuntu user below lines to compile the program via using standart packages may be helpful to understand the root cause
sudo apt-get install libdxflib-dev libdxflib-2.5.0.0
replace inclusions with
#include<dxflib/dl_dxf.h>
#include<dxflib/dl_creationadapter.h>
then compile with command
g++ -Wall dxf.cpp -ldxflib -L/usr/lib/x86_64-linux-gnu/
and run a.out
Note that /usr/lib/x86_64-linux-gnu/ is where libdxflib.so stands. It may be different in your environment.
I wrote Snake in SDL and now want to port it to Android, which means I had to rewrite parts of it to use SDL2. I replaced the key control and rendering parts and it compiles without any errors. However, when I try to run it it crashes immediately. I ran it with the debugger, but it doesn't give any useful information:
(no debugging symbols found)
Program received signal SIGSEGV,Segmentation fault
In ?? () ()
I set a breakpoint on the first line of the code and it doesn't even reach that before crashing. I am also using SDL_image and SDL_ttf.
Source code:
http://www.mediafire.com/?n0zdd061d343w35
Maybe the error is on the SDL loading, you can not resolve the symbols and get an error.
I'm getting this:
Program received signal SIGSEGV, Segmentation fault.
0x00007ffff7580d75 in ?? () from /lib/x86_64-linux-gnu/libc.so.6
Can I know why the seg fault is occuring?
I am currently printing the content of a C++ map in a file when this error occurs. The map size would be in MBs, could that be a problem?
Try installing debugging symbols for libc, they should be available as a package. In ubuntu they can be found in the package libc-dbg. Gdb should produce better output for you then.
How do I debug a segmentation fault?
Basically this is what happens:
I run my server in background: ./server &
then I run my client: ./client
When I try to login to my server, on correct username and password, everything is okay, but when I type invalid user and password, it results in a segmentation fault.
How do I make the compiler/debugger able to output what error its actually see that causes segmentation core dump.
I know gdb but I try using gdb client but it doesn't seem to work.
A good idea with segmentation faults is to run the program with valgrind for debugging. That way, you'll often get more detailed information about what caused your segmentation fault. For example, it will tell you if you are reading from uninitialized memory.
If you are using g++ first compile your program using the -g option. Then use
gdb name_of_program core
to run gdb on the core dump you get (name_of_program is the name of the executable file you just built with g++).
This link is useful for how to use gdb.
http://www.ibm.com/developerworks/library/l-gdb/
this ads annotations to the code. it's helpful only if you have a lot of function calls and you don't know the call path.
I have c/gtk+ application and GList which filled three elements, when i try to run following code with gdb:
if (g_list_length(mw->img_list) > 0)
printf(">0");
else
printf("<0");
i see:
Program received signal SIGSEGV, Segmentation fault.
[Switching to Thread 0xb73c4700 (LWP 7936)]
IA__g_list_length (list=0x6e6920) at glist.c:767
767 glist.c: No such file or directory.
in glist.c
What is it?
Thank you.
This is a crash in glib, probably because you're handing it a bad pointer to a list. The debugger tries to load the source code to show you where it crashed, but can't find it (probably because you just linked to the lib, without even having the source handy).
Use the gdb up command to step upwards in the call stack until you reach your code, and inspect the argument you passed to the g_list_length() function.
Your debugger is trying to find the source code for GList to help you to debug the problem. Typically, you won't have the source installed. You'll need to install debugging packages or source of some kind.
If you are on a Fedora system, debuginfo-install glib2 will do it. On Debian or Ubuntu, there may be a package for this, possibly ending in -dbg?
It looks as though its trying to find something(on your hard disk)that doesn't exist. Is that all of the code?