Skip to content

Commit f7450ba

Browse files
committed
catch std::bad_alloc and update comment
1 parent 69256af commit f7450ba

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

src/main.cc

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,28 +54,35 @@ int main(int argc, char* args[]) {
5454
return EXIT_FAILURE;
5555
}
5656

57+
5758
try {
5859
// Try to perform error recovery from the snapshot if necessary and possible.
5960
if (wm->snapshot().FileExists()) {
6061
wm->snapshot().Load();
6162
}
63+
wm->Run(); // enter main event loop
64+
65+
} catch (const std::bad_alloc& ex) {
66+
fputs("Out of memory\n", stderr);
67+
return EXIT_FAILURE;
6268

63-
// Run the window manager.
64-
wm->Run();
6569
} catch (const wmderland::Snapshot::SnapshotLoadError& ex) {
6670
// If we cannot recover from errors using the snapshot,
6771
// then return with EXIT_FAILURE.
6872
WM_LOG(ERROR, ex.what());
73+
std::cerr << ex.what() << std::endl;
6974
rename(wm->snapshot().filename().c_str(), (wm->snapshot().filename() + ".failed_to_load").c_str());
7075
return EXIT_FAILURE;
76+
7177
} catch (const std::exception& ex) {
72-
// Try to exec itself and recover from errors the snapshot.
78+
// Try to exec itself and recover from errors using the snapshot.
7379
// If snapshot fails to load, it will throw an SnapshotLoadError.
7480
// See the previous catch block.
7581
WM_LOG(ERROR, ex.what());
7682
wmderland::sys_utils::NotifySend("An error occurred. Recovering...", NOTIFY_SEND_CRITICAL);
7783
wm->snapshot().Save();
7884
execl(args[0], args[0], nullptr);
85+
7986
} catch (...) {
8087
WM_LOG(ERROR, "Unknown exception caught!");
8188
return EXIT_FAILURE;

src/window_manager.cc

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ extern "C" {
1414
#include <fstream>
1515

1616
#include "client.h"
17+
#include "config.h"
1718
#include "util.h"
1819

1920
#define MOUSE_LEFT_BTN 1
@@ -41,7 +42,12 @@ bool WindowManager::is_running_ = true;
4142
WindowManager* WindowManager::GetInstance() {
4243
if (!instance_) {
4344
Display* dpy;
44-
instance_ = (dpy = XOpenDisplay(None)) ? new WindowManager(dpy) : nullptr;
45+
try {
46+
instance_ = (dpy = XOpenDisplay(None)) ? new WindowManager(dpy) : nullptr;
47+
} catch (const std::bad_alloc& ex) {
48+
fputs("Out of memory\n", stderr);
49+
instance_ = nullptr;
50+
}
4551
}
4652
return instance_;
4753
}

0 commit comments

Comments
 (0)