Skip to content

Commit

Permalink
Changed the control flow in main to allow a try/catch of a bad_alloc …
Browse files Browse the repository at this point in the history
…exception.
  • Loading branch information
DanielOlson committed Sep 13, 2024
1 parent c0fdff8 commit 7ccf1ea
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/cli.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#ifndef ULTRA_CLI_HPP
#define ULTRA_CLI_HPP

#define ULTRA_VERSION_STRING "1.0.0"
#define ULTRA_VERSION_STRING "1.0.2"


#include "../lib/CLI11.hpp"
Expand Down
31 changes: 30 additions & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
#include "mask.hpp"
#include "ultra.hpp"
#include <string>
#include <new> // for std::bad_alloc

int main(int argc, const char *argv[]) {

int main_wrapper(int argc, const char * argv[]) {
// Prepare settings
Settings *settings = new Settings();
settings->prepare_settings();
Expand Down Expand Up @@ -172,3 +173,31 @@ int main(int argc, const char *argv[]) {

return 0;
}


int main(int argc, const char *argv[]) {

char *reserve_memory = (char *)malloc(65536);

try {
int r = main_wrapper(argc, argv);
return r;
}
catch (const std::bad_alloc& e) {
// This block is executed if memory allocation fails
free(reserve_memory); // May be necessary in order to print
std::cerr << "Memory allocation failed: " << e.what() << std::endl;
std::cerr << "Your model may be too large to fit in memory" << std::endl;
std::cerr << "Try running: ultra --mem <your arguments> to see expected memory usage" << std::endl;
}
catch (const std::exception& e) {
// This block is executed for any other standard exceptions
std::cerr << "Standard exception caught: " << e.what() << std::endl;
}
catch (...) {
// This block catches any other non-standard exceptions
std::cerr << "Unknown exception caught!" << std::endl;
}

return -1;
}

0 comments on commit 7ccf1ea

Please sign in to comment.