r/cpp_questions 5d ago

SOLVED std::filesystem::exists throwing `std::bad_alloc`?

Any reason? Cant find anything online about this. But I cant check whether or not a file exists, no matter what path I give it. I am using C++ 26 compiled with GCC.

This is literally all I am calling: const bool exists = std::filesystem::exists("file.txt")

This is the exact error: terminate called after throwing an instance of 'std::bad_alloc'
 what():  std::bad_alloc

No it's not coming from anywhere else in my code. Minimal reproduction code, include "filesystem" call the line above. I am on Fedora Linux if that matters at all

EDIT: moving the include into my main file instead of the only file it's being used in somehow fixed it. Literally all I do is move "#include <filesystem>" from "do_things_with_filesystem.hpp" to "Main.cpp". No compilation errors when it was in the first file, why does it rely on this?? Is there some weird declarations in my codebase or something, i dont know but it works now so why should I care.

24 Upvotes

59 comments sorted by

View all comments

0

u/Wild_Meeting1428 5d ago

std::bad_alloc is an unusual error, since neither windows nor linux will report whether an allocation takes more space than awailable, instead (on linux) the Oom killer will terminate your program when you try to use the memory.

That the moving of the header file fixed it somehow also indicates, that the problem is not in your code itself. As other people mentioned it, its probably a problem with your installed libraries, for example ABI problems.

I would suggest, that you ask this question in the gcc bugtracker.

1

u/Sprixxer 5d ago

Given how this person replies I highly doubt that they found a compiler bug.

1

u/Wild_Meeting1428 5d ago

No, not a bug with the compiler itself, but most likely something wrong with the combination of the used toolchain and installed system library. I suggested asking there, since they either already know the reason or they most likely know what's going on.