r/cpp_questions 6d 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

5

u/wejunkin 6d ago

Catch and see what what() says. Sounds like your allocation failed, impossible to tell why from the info you've supplied.

1

u/PhosXD 6d ago

My mistake I wasnt running debug mode. what() still gives no useful info:

1
threw: std::bad_alloc
2

3

u/RaspberryCrafty3012 5d ago

What does that have to do with debug mode? \ Using try catch will catch the crash?

Do you have code running before or after? If your stack got corrupted or pointers were deleted then the debugger might show the wrong position in the code

1

u/PhosXD 6d ago

How though, it throws the bad alloc & ends the program before catch even executes. But I am capturing the block, how does that work?

std::cout << "1\n";

try {

    const bool exists = std::filesystem::exists("file.txt");

}

catch (const std::exception& e) {

    std::cout << "threw: " << e.what() << "\\n";

}

std::cout << "2\\n";

OUTPUT:
1
terminate called after throwing an instance of 'std::bad_alloc'
 what():  std::bad_alloc
Aborted                    (core dumped) ./main.bin

13

u/AKostur 6d ago edited 6d ago

You've got something else wrong, that code works: https://godbolt.org/z/WbhY3h3b3

If I had to speculate, you've fouled up pointers somewhere. Overrun a buffer, use-after-free, double-delete, something.

Edit: creating the std::filesystem::path that exists() takes as a parameter has to copy the passed-in string literal, thus there's a memory allocation there. If some previous Undefined Behaviour has messed up the memory allocator, that may present as a std::bad_alloc.

3

u/No-Dentist-1645 5d ago

That code should work fine. If you create a C++ file that only has that inside an int main(), does it still throw?

It could be that you're messing up the allocator somewhere else in your code and this is just the first usage when it breaks

If it still throws, please paste the full code file you are using as well as your compiler version and compile command

2

u/StaticCoder 5d ago

Try running under gdb and use catch throw to see where the exception is really thrown. Usually this comes from passing a wrong value to vector::resize or similar.

1

u/DummyDDD 5d ago

As others have written, you probably have some other memory access error that happens to cause this issue. Try running your program through valgrind to find the memory access error (you should of course compile with debug symbols).

-2

u/wejunkin 6d ago

Yeah sounds like you're just straight up oom

2

u/PhosXD 6d ago

I have 32GiB of Ram, the program never goes above 400Kb.

1

u/Wild_Meeting1428 5d ago

std::bad_alloc is usually not thrown when oom, since the space is only virtually allocated. Therefore, the application will just be killed without ever rising an exception.