diff --git a/src/fapp.cpp b/src/fapp.cpp index a08866ee..e1db28f4 100644 --- a/src/fapp.cpp +++ b/src/fapp.cpp @@ -312,8 +312,17 @@ void FApplication::init() gpm_ev.x = -1; #endif - zero_point = new FPoint(0,0); - event_queue = new eventQueue; + try + { + zero_point = new FPoint(0,0); + event_queue = new eventQueue; + } + catch (const std::bad_alloc& ex) + { + std::cerr << "not enough memory to alloc " << ex.what() << std::endl; + std::abort(); + } + // init arrays with '\0' std::fill_n (k_buf, sizeof(k_buf), '\0'); std::fill_n (fifo_buf, fifo_buf_size, '\0'); diff --git a/src/fbutton.cpp b/src/fbutton.cpp index 0537e87f..b723581e 100644 --- a/src/fbutton.cpp +++ b/src/fbutton.cpp @@ -253,7 +253,15 @@ void FButton::hide() if ( size < 0 ) return; - blank = new char[size+1]; + try + { + blank = new char[size+1]; + } + catch (const std::bad_alloc& ex) + { + std::cerr << "not enough memory to alloc " << ex.what() << std::endl; + return; + } std::memset(blank, ' ', uLong(size)); blank[size] = '\0'; @@ -502,10 +510,18 @@ void FButton::draw() hotkey_offset = 0; space = int(' '); - if ( isMonochron() || getMaxColor() < 16 ) - ButtonText = new wchar_t[length+3](); - else - ButtonText = new wchar_t[length+1](); + try + { + if ( isMonochron() || getMaxColor() < 16 ) + ButtonText = new wchar_t[length+3](); + else + ButtonText = new wchar_t[length+1](); + } + catch (const std::bad_alloc& ex) + { + std::cerr << "not enough memory to alloc " << ex.what() << std::endl; + return; + } txt = FString(text); src = const_cast(txt.wc_str()); diff --git a/src/fbuttongroup.cpp b/src/fbuttongroup.cpp index efdf81d8..f19324d8 100644 --- a/src/fbuttongroup.cpp +++ b/src/fbuttongroup.cpp @@ -206,7 +206,16 @@ void FButtonGroup::hide() if ( size < 0 ) return; - blank = new char[size+1]; + try + { + blank = new char[size+1]; + } + catch (const std::bad_alloc& ex) + { + std::cerr << "not enough memory to alloc " << ex.what() << std::endl; + return; + } + std::memset(blank, ' ', uLong(size)); blank[size] = '\0'; @@ -491,7 +500,17 @@ void FButtonGroup::drawLabel() txt = " " + text + " "; length = txt.getLength(); hotkeypos = -1; - LabelText = new wchar_t[length+1](); + + try + { + LabelText = new wchar_t[length+1](); + } + catch (const std::bad_alloc& ex) + { + std::cerr << "not enough memory to alloc " << ex.what() << std::endl; + return; + } + src = const_cast(txt.wc_str()); dest = const_cast(LabelText); diff --git a/src/fdialog.cpp b/src/fdialog.cpp index 1ed76499..05af75f6 100644 --- a/src/fdialog.cpp +++ b/src/fdialog.cpp @@ -767,11 +767,20 @@ void FDialog::onMouseMove (FMouseEvent* ev) const FPoint& g = ev->getTermPos(); const FPoint& p = dialog_menu->termToWidgetPos(g); int b = ev->getButton(); - FMouseEvent* _ev = new FMouseEvent (fc::MouseMove_Event, p, g, b); - dialog_menu->mouse_down = true; - setClickedWidget(dialog_menu); - dialog_menu->onMouseMove(_ev); - delete _ev; + + try + { + FMouseEvent* _ev = new FMouseEvent (fc::MouseMove_Event, p, g, b); + dialog_menu->mouse_down = true; + setClickedWidget(dialog_menu); + dialog_menu->onMouseMove(_ev); + delete _ev; + } + catch (const std::bad_alloc& ex) + { + std::cerr << "not enough memory to alloc " << ex.what() << std::endl; + return; + } } } @@ -1058,9 +1067,27 @@ void FDialog::init() old_focus->redraw(); } - accelerator_list = new Accelerators(); + try + { + accelerator_list = new Accelerators(); + } + catch (const std::bad_alloc& ex) + { + std::cerr << "not enough memory to alloc " << ex.what() << std::endl; + return; + } + // Add the dialog menu - dialog_menu = new FMenu ("-", this); + try + { + dialog_menu = new FMenu ("-", this); + } + catch (const std::bad_alloc& ex) + { + std::cerr << "not enough memory to alloc " << ex.what() << std::endl; + return; + } + dialog_menu->setPos (getX(), getY()+1); dgl_menuitem = dialog_menu->getItem(); @@ -1070,7 +1097,16 @@ void FDialog::init() dgl_menuitem->unsetFocusable(); } - move_size_item = new FMenuItem (dialog_menu); + try + { + move_size_item = new FMenuItem (dialog_menu); + } + catch (const std::bad_alloc& ex) + { + std::cerr << "not enough memory to alloc " << ex.what() << std::endl; + return; + } + move_size_item->setText ("&Move/Size"); move_size_item->setStatusbarMessage ("Move or change the size of the window"); @@ -1080,7 +1116,16 @@ void FDialog::init() F_METHOD_CALLBACK (this, &FDialog::cb_move) ); - zoom_item = new FMenuItem (dialog_menu); + try + { + zoom_item = new FMenuItem (dialog_menu); + } + catch (const std::bad_alloc& ex) + { + std::cerr << "not enough memory to alloc " << ex.what() << std::endl; + return; + } + setZoomItem(); zoom_item->setDisable(); @@ -1090,7 +1135,16 @@ void FDialog::init() F_METHOD_CALLBACK (this, &FDialog::cb_zoom) ); - close_item = new FMenuItem ("&Close", dialog_menu); + try + { + close_item = new FMenuItem ("&Close", dialog_menu); + } + catch (const std::bad_alloc& ex) + { + std::cerr << "not enough memory to alloc " << ex.what() << std::endl; + return; + } + close_item->setStatusbarMessage ("Close this window"); close_item->addCallback @@ -1421,15 +1475,24 @@ void FDialog::cb_move (FWidget*, data_ptr) setMoveSizeWidget(this); if ( isMonochron() ) - setReverse(true); + setReverse(true); drawBorder(); if ( isMonochron() ) - setReverse(false); + setReverse(false); save_geometry = getGeometry(); - tooltip = new FToolTip(this); + + try + { + tooltip = new FToolTip(this); + } + catch (const std::bad_alloc& ex) + { + std::cerr << "not enough memory to alloc " << ex.what() << std::endl; + return; + } if ( isResizeable() ) { diff --git a/src/ffiledialog.cpp b/src/ffiledialog.cpp index 6b4c1cf4..1df3e7ad 100644 --- a/src/ffiledialog.cpp +++ b/src/ffiledialog.cpp @@ -375,10 +375,18 @@ const FString FFileDialog::fileOpenChooser ( FWidget* parent if ( file_filter.isNull() || file_filter.isEmpty() ) file_filter = FString("*"); - fileopen = new FFileDialog ( path - , file_filter - , FFileDialog::Open - , parent ); + try + { + fileopen = new FFileDialog ( path + , file_filter + , FFileDialog::Open + , parent ); + } + catch (const std::bad_alloc& ex) + { + std::cerr << "not enough memory to alloc " << ex.what() << std::endl; + return FString(); + } if ( fileopen->exec() == FDialog::Accept ) ret = fileopen->getPath() + fileopen->getSelectedFile(); @@ -410,10 +418,18 @@ const FString FFileDialog::fileSaveChooser ( FWidget* parent if ( file_filter.isNull() || file_filter.isEmpty() ) file_filter = FString("*"); - fileopen = new FFileDialog ( path - , file_filter - , FFileDialog::Save - , parent ); + try + { + fileopen = new FFileDialog ( path + , file_filter + , FFileDialog::Save + , parent ); + } + catch (const std::bad_alloc& ex) + { + std::cerr << "not enough memory to alloc " << ex.what() << std::endl; + return FString(); + } if ( fileopen->exec() == FDialog::Accept ) ret = fileopen->getPath() + fileopen->getSelectedFile(); @@ -490,29 +506,37 @@ void FFileDialog::init() else FDialog::setText("Open file"); - filename = new FLineEdit(this); - filename->setLabelText("File&name"); - filename->setText(filter_pattern); - filename->setGeometry(11, 1, 28, 1); - filename->setFocus(); + try + { + filename = new FLineEdit(this); + filename->setLabelText("File&name"); + filename->setText(filter_pattern); + filename->setGeometry(11, 1, 28, 1); + filename->setFocus(); - filebrowser = new FListBox(this); - filebrowser->setGeometry(2, 3, 38, 6); - printPath(directory); + filebrowser = new FListBox(this); + filebrowser->setGeometry(2, 3, 38, 6); + printPath(directory); - hidden = new FCheckBox("&hidden files", this); - hidden->setGeometry(2, 10, 16, 1); + hidden = new FCheckBox("&hidden files", this); + hidden->setGeometry(2, 10, 16, 1); - cancel = new FButton("&Cancel", this); - cancel->setGeometry(19, 10, 9, 1); + cancel = new FButton("&Cancel", this); + cancel->setGeometry(19, 10, 9, 1); - if ( dlg_type == FFileDialog::Save ) - open = new FButton("&Save",this); - else - open = new FButton("&Open",this); + if ( dlg_type == FFileDialog::Save ) + open = new FButton("&Save",this); + else + open = new FButton("&Open",this); - open->setGeometry(30, 10, 9, 1); - setGeometry (x, y, getWidth(), getHeight()); + open->setGeometry(30, 10, 9, 1); + setGeometry (x, y, getWidth(), getHeight()); + } + catch (const std::bad_alloc& ex) + { + std::cerr << "not enough memory to alloc " << ex.what() << std::endl; + return; + } filename->addCallback ( diff --git a/src/flabel.cpp b/src/flabel.cpp index e98b2bbe..018fa38e 100644 --- a/src/flabel.cpp +++ b/src/flabel.cpp @@ -158,7 +158,16 @@ void FLabel::hide() if ( size < 0 ) return; - blank = new char[size+1]; + try + { + blank = new char[size+1]; + } + catch (const std::bad_alloc& ex) + { + std::cerr << "not enough memory to alloc " << ex.what() << std::endl; + return; + } + std::memset(blank, ' ', uLong(size)); blank[getWidth()] = '\0'; setPrintPos (1,1); @@ -180,9 +189,18 @@ void FLabel::onMouseDown (FMouseEvent* ev) int b = ev->getButton(); const FPoint& tp = ev->getTermPos(); const FPoint& p = parent->termToWidgetPos(tp); - FMouseEvent* _ev = new FMouseEvent (fc::MouseDown_Event, p, tp, b); - FApplication::sendEvent (parent, _ev); - delete _ev; + + try + { + FMouseEvent* _ev = new FMouseEvent (fc::MouseDown_Event, p, tp, b); + FApplication::sendEvent (parent, _ev); + delete _ev; + } + catch (const std::bad_alloc& ex) + { + std::cerr << "not enough memory to alloc " << ex.what() << std::endl; + return; + } } return; @@ -468,7 +486,17 @@ void FLabel::draw() while ( y < text_lines && y < uInt(getHeight()) ) { length = multiline_text[y].getLength(); - LabelText = new wchar_t[length+1](); + + try + { + LabelText = new wchar_t[length+1](); + } + catch (const std::bad_alloc& ex) + { + std::cerr << "not enough memory to alloc " << ex.what() << std::endl; + return; + } + src = const_cast(multiline_text[y].wc_str()); dest = const_cast(LabelText); @@ -499,7 +527,17 @@ void FLabel::draw() else { length = text.getLength(); - LabelText = new wchar_t[length+1](); + + try + { + LabelText = new wchar_t[length+1](); + } + catch (const std::bad_alloc& ex) + { + std::cerr << "not enough memory to alloc " << ex.what() << std::endl; + return; + } + src = const_cast(text.wc_str()); dest = const_cast(LabelText); hotkeypos = getHotkeyPos (src, dest, length); diff --git a/src/flineedit.cpp b/src/flineedit.cpp index 6d9e71a4..fef778f7 100644 --- a/src/flineedit.cpp +++ b/src/flineedit.cpp @@ -193,7 +193,16 @@ void FLineEdit::hide() if ( size < 0 ) return; - blank = new char[size+1]; + try + { + blank = new char[size+1]; + } + catch (const std::bad_alloc& ex) + { + std::cerr << "not enough memory to alloc " << ex.what() << std::endl; + return; + } + std::memset(blank, ' ', uLong(size)); blank[size] = '\0'; diff --git a/src/flistbox.cpp b/src/flistbox.cpp index b56d4eab..3a414fde 100644 --- a/src/flistbox.cpp +++ b/src/flistbox.cpp @@ -243,7 +243,16 @@ void FListBox::hide() if ( size < 0 ) return; - blank = new char[size+1]; + try + { + blank = new char[size+1]; + } + catch (const std::bad_alloc& ex) + { + std::cerr << "not enough memory to alloc " << ex.what() << std::endl; + return; + } + std::memset (blank, ' ', uLong(size)); blank[size] = '\0'; @@ -369,7 +378,16 @@ void FListBox::clear() if ( size < 0 ) return; - blank = new char[size+1]; + try + { + blank = new char[size+1]; + } + catch (const std::bad_alloc& ex) + { + std::cerr << "not enough memory to alloc " << ex.what() << std::endl; + return; + } + std::memset (blank, ' ', uLong(size)); blank[size] = '\0'; @@ -1225,15 +1243,23 @@ void FListBox::init() setForegroundColor (wc.dialog_fg); setBackgroundColor (wc.dialog_bg); - vbar = new FScrollbar(fc::vertical, this); - vbar->setMinimum(0); - vbar->setValue(0); - vbar->hide(); + try + { + vbar = new FScrollbar(fc::vertical, this); + vbar->setMinimum(0); + vbar->setValue(0); + vbar->hide(); - hbar = new FScrollbar(fc::horizontal, this); - hbar->setMinimum(0); - hbar->setValue(0); - hbar->hide(); + hbar = new FScrollbar(fc::horizontal, this); + hbar->setMinimum(0); + hbar->setValue(0); + hbar->hide(); + } + catch (const std::bad_alloc& ex) + { + std::cerr << "not enough memory to alloc " << ex.what() << std::endl; + return; + } setGeometry (1, 1, 5, 4, false); // initialize geometry values diff --git a/src/flistview.cpp b/src/flistview.cpp index 2abb2645..2c983355 100644 --- a/src/flistview.cpp +++ b/src/flistview.cpp @@ -282,10 +282,18 @@ void FListView::insert ( const std::vector& cols , data_ptr d , FListView* parent ) { - if ( parent == 0 ) - new FListViewItem (cols, d, this); - else - new FListViewItem (cols, d, parent); + try + { + if ( parent == 0 ) + new FListViewItem (cols, d, this); + else + new FListViewItem (cols, d, parent); + } + catch (const std::bad_alloc& ex) + { + std::cerr << "not enough memory to alloc " << ex.what() << std::endl; + return; + } } //---------------------------------------------------------------------- @@ -874,15 +882,23 @@ void FListView::init() setForegroundColor (wc.dialog_fg); setBackgroundColor (wc.dialog_bg); - vbar = new FScrollbar(fc::vertical, this); - vbar->setMinimum(0); - vbar->setValue(0); - vbar->hide(); + try + { + vbar = new FScrollbar(fc::vertical, this); + vbar->setMinimum(0); + vbar->setValue(0); + vbar->hide(); - hbar = new FScrollbar(fc::horizontal, this); - hbar->setMinimum(0); - hbar->setValue(0); - hbar->hide(); + hbar = new FScrollbar(fc::horizontal, this); + hbar->setMinimum(0); + hbar->setValue(0); + hbar->hide(); + } + catch (const std::bad_alloc& ex) + { + std::cerr << "not enough memory to alloc " << ex.what() << std::endl; + return; + } setGeometry (1, 1, 5, 4, false); // initialize geometry values diff --git a/src/fmenu.cpp b/src/fmenu.cpp index 8fead790..48322287 100644 --- a/src/fmenu.cpp +++ b/src/fmenu.cpp @@ -34,7 +34,16 @@ FMenu::FMenu (const FString& txt, FWidget* parent) , mouse_down(false) , has_checkable_items(false) { - item = new FMenuItem(txt, parent); + try + { + item = new FMenuItem(txt, parent); + } + catch (const std::bad_alloc& ex) + { + std::cerr << "not enough memory to alloc " << ex.what() << std::endl; + return; + } + init(parent); } @@ -582,11 +591,20 @@ void FMenu::onMouseMove (FMouseEvent* ev) const FPoint& t = ev->getTermPos(); const FPoint& p = open_sub_menu->termToWidgetPos(t); int b = ev->getButton(); - FMouseEvent* _ev = new FMouseEvent (fc::MouseMove_Event, p, t, b); - open_sub_menu->mouse_down = true; - setClickedWidget(open_sub_menu); - open_sub_menu->onMouseMove(_ev); - delete _ev; + + try + { + FMouseEvent* _ev = new FMouseEvent (fc::MouseMove_Event, p, t, b); + open_sub_menu->mouse_down = true; + setClickedWidget(open_sub_menu); + open_sub_menu->onMouseMove(_ev); + delete _ev; + } + catch (const std::bad_alloc& ex) + { + std::cerr << "not enough memory to alloc " << ex.what() << std::endl; + } + return; } else if ( ! mouse_over_menu && mouse_over_supermenu ) @@ -595,11 +613,20 @@ void FMenu::onMouseMove (FMouseEvent* ev) const FPoint& t = ev->getTermPos(); const FPoint& p = smenu->termToWidgetPos(t); int b = ev->getButton(); - FMouseEvent* _ev = new FMouseEvent (fc::MouseMove_Event, p, t, b); - smenu->mouse_down = true; - setClickedWidget(smenu); - smenu->onMouseMove(_ev); - delete _ev; + + try + { + FMouseEvent* _ev = new FMouseEvent (fc::MouseMove_Event, p, t, b); + smenu->mouse_down = true; + setClickedWidget(smenu); + smenu->onMouseMove(_ev); + delete _ev; + } + catch (const std::bad_alloc& ex) + { + std::cerr << "not enough memory to alloc " << ex.what() << std::endl; + } + return; } else if ( mouse_over_menubar ) @@ -609,12 +636,21 @@ void FMenu::onMouseMove (FMouseEvent* ev) const FPoint& t = ev->getTermPos(); const FPoint& p = menubar->termToWidgetPos(t); int b = ev->getButton(); - FMouseEvent* _ev = new FMouseEvent (fc::MouseMove_Event, p, t, b); - setClickedWidget(menubar); - FMenuBar* mbar = reinterpret_cast(menubar); - mbar->mouse_down = true; - mbar->onMouseMove(_ev); - delete _ev; + + try + { + FMouseEvent* _ev = new FMouseEvent (fc::MouseMove_Event, p, t, b); + setClickedWidget(menubar); + FMenuBar* mbar = reinterpret_cast(menubar); + mbar->mouse_down = true; + mbar->onMouseMove(_ev); + delete _ev; + } + catch (const std::bad_alloc& ex) + { + std::cerr << "not enough memory to alloc " << ex.what() << std::endl; + } + return; } else if ( ! hasSelectedItem() && mouse_over_menu ) @@ -1307,7 +1343,17 @@ void FMenu::drawItems() print (' '); txt = (*iter)->getText(); txt_length = uInt(txt.getLength()); - item_text = new wchar_t[txt_length+1](); + + try + { + item_text = new wchar_t[txt_length+1](); + } + catch (const std::bad_alloc& ex) + { + std::cerr << "not enough memory to alloc " << ex.what() << std::endl; + return; + } + src = const_cast(txt.wc_str()); dest = const_cast(item_text); to_char = int(txt_length); diff --git a/src/fmenubar.cpp b/src/fmenubar.cpp index f31aa125..0e7ec753 100644 --- a/src/fmenubar.cpp +++ b/src/fmenubar.cpp @@ -43,7 +43,16 @@ void FMenuBar::hide() if ( screenWidth < 0 ) return; - blank = new char[screenWidth+1]; + try + { + blank = new char[screenWidth+1]; + } + catch (const std::bad_alloc& ex) + { + std::cerr << "not enough memory to alloc " << ex.what() << std::endl; + return; + } + std::memset(blank, ' ', uLong(screenWidth)); blank[screenWidth] = '\0'; setPrintPos (1,1); @@ -408,11 +417,19 @@ void FMenuBar::onMouseMove (FMouseEvent* ev) const FPoint& t = ev->getTermPos(); const FPoint& p = menu->termToWidgetPos(t); int b = ev->getButton(); - _ev = new FMouseEvent (fc::MouseMove_Event, p, t, b); - menu->mouse_down = true; - setClickedWidget(menu); - menu->onMouseMove(_ev); - delete _ev; + + try + { + _ev = new FMouseEvent (fc::MouseMove_Event, p, t, b); + menu->mouse_down = true; + setClickedWidget(menu); + menu->onMouseMove(_ev); + delete _ev; + } + catch (const std::bad_alloc& ex) + { + std::cerr << "not enough memory to alloc " << ex.what() << std::endl; + } } } } @@ -799,7 +816,17 @@ void FMenuBar::drawItems() txt = (*iter)->getText(); txt_length = uInt(txt.getLength()); - item_text = new wchar_t[txt_length+1](); + + try + { + item_text = new wchar_t[txt_length+1](); + } + catch (const std::bad_alloc& ex) + { + std::cerr << "not enough memory to alloc " << ex.what() << std::endl; + return; + } + src = const_cast(txt.wc_str()); dest = const_cast(item_text); diff --git a/src/fmenuitem.cpp b/src/fmenuitem.cpp index 89a086c6..0903c1a1 100644 --- a/src/fmenuitem.cpp +++ b/src/fmenuitem.cpp @@ -333,9 +333,17 @@ void FMenuItem::onMouseDoubleClick (FMouseEvent* ev) if ( smenu ) { const FPoint& p2 = smenu->termToWidgetPos(t); - FMouseEvent* _ev = new FMouseEvent (fc::MouseDoubleClick_Event, p2, t, b); - smenu->onMouseDoubleClick(_ev); - delete _ev; + + try + { + FMouseEvent* _ev = new FMouseEvent (fc::MouseDoubleClick_Event, p2, t, b); + smenu->onMouseDoubleClick(_ev); + delete _ev; + } + catch (const std::bad_alloc& ex) + { + std::cerr << "not enough memory to alloc " << ex.what() << std::endl; + } } } @@ -346,9 +354,17 @@ void FMenuItem::onMouseDoubleClick (FMouseEvent* ev) if ( mbar ) { const FPoint& p2 = mbar->termToWidgetPos(t); - FMouseEvent* _ev = new FMouseEvent (fc::MouseDoubleClick_Event, p2, t, b); - mbar->onMouseDoubleClick(_ev); - delete _ev; + + try + { + FMouseEvent* _ev = new FMouseEvent (fc::MouseDoubleClick_Event, p2, t, b); + mbar->onMouseDoubleClick(_ev); + delete _ev; + } + catch (const std::bad_alloc& ex) + { + std::cerr << "not enough memory to alloc " << ex.what() << std::endl; + } } } @@ -359,9 +375,17 @@ void FMenuItem::onMouseDoubleClick (FMouseEvent* ev) if ( dgl ) { const FPoint& p2 = dgl->termToWidgetPos(t); - FMouseEvent* _ev = new FMouseEvent (fc::MouseDoubleClick_Event, p2, t, b); - dgl->onMouseDoubleClick(_ev); - delete _ev; + + try + { + FMouseEvent* _ev = new FMouseEvent (fc::MouseDoubleClick_Event, p2, t, b); + dgl->onMouseDoubleClick(_ev); + delete _ev; + } + catch (const std::bad_alloc& ex) + { + std::cerr << "not enough memory to alloc " << ex.what() << std::endl; + } } } } @@ -382,9 +406,17 @@ void FMenuItem::onMouseDown (FMouseEvent* ev) if ( smenu ) { const FPoint& p2 = smenu->termToWidgetPos(t); - FMouseEvent* _ev = new FMouseEvent (fc::MouseDown_Event, p2, t, b); - smenu->onMouseDown(_ev); - delete _ev; + + try + { + FMouseEvent* _ev = new FMouseEvent (fc::MouseDown_Event, p2, t, b); + smenu->onMouseDown(_ev); + delete _ev; + } + catch (const std::bad_alloc& ex) + { + std::cerr << "not enough memory to alloc " << ex.what() << std::endl; + } } } @@ -395,9 +427,17 @@ void FMenuItem::onMouseDown (FMouseEvent* ev) if ( mbar ) { const FPoint& p2 = mbar->termToWidgetPos(t); - FMouseEvent* _ev = new FMouseEvent (fc::MouseDown_Event, p2, t, b); - mbar->onMouseDown(_ev); - delete _ev; + + try + { + FMouseEvent* _ev = new FMouseEvent (fc::MouseDown_Event, p2, t, b); + mbar->onMouseDown(_ev); + delete _ev; + } + catch (const std::bad_alloc& ex) + { + std::cerr << "not enough memory to alloc " << ex.what() << std::endl; + } } } @@ -408,9 +448,17 @@ void FMenuItem::onMouseDown (FMouseEvent* ev) if ( dgl ) { const FPoint& p2 = dgl->termToWidgetPos(t); - FMouseEvent* _ev = new FMouseEvent (fc::MouseDown_Event, p2, t, b); - dgl->onMouseDown(_ev); - delete _ev; + + try + { + FMouseEvent* _ev = new FMouseEvent (fc::MouseDown_Event, p2, t, b); + dgl->onMouseDown(_ev); + delete _ev; + } + catch (const std::bad_alloc& ex) + { + std::cerr << "not enough memory to alloc " << ex.what() << std::endl; + } } } } @@ -734,11 +782,20 @@ void FMenuItem::createDialogList (FMenu* winmenu) if ( win ) { + FMenuItem* win_item; int n = int(std::distance(begin, iter)); // get the dialog title const FString& name = win->getText(); - // create a new dialog list item - FMenuItem* win_item = new FMenuItem (name, winmenu); + + try + { + // create a new dialog list item + win_item = new FMenuItem (name, winmenu); + } + catch (const std::bad_alloc& ex) + { + std::cerr << "not enough memory to alloc " << ex.what() << std::endl; + } if ( n < 9 ) win_item->addAccelerator (fc::Fmkey_1 + n); // Meta + 1..9 diff --git a/src/fmessagebox.cpp b/src/fmessagebox.cpp index 8fb44262..7da6cb82 100644 --- a/src/fmessagebox.cpp +++ b/src/fmessagebox.cpp @@ -170,9 +170,20 @@ int FMessageBox::info ( FWidget* parent , int button2 ) { int reply; - FMessageBox* mbox = new FMessageBox ( caption, message - , button0, button1, button2 - , parent ); + FMessageBox* mbox; + + try + { + mbox = new FMessageBox ( caption, message + , button0, button1, button2 + , parent ); + } + catch (const std::bad_alloc& ex) + { + std::cerr << "not enough memory to alloc " << ex.what() << std::endl; + return FDialog::Reject; + } + reply = mbox->exec(); delete mbox; return reply; @@ -187,10 +198,21 @@ int FMessageBox::info ( FWidget* parent , int button2 ) { int reply; - FMessageBox* mbox = new FMessageBox ( caption - , FString().setNumber(num) - , button0, button1, button2 - , parent ); + FMessageBox* mbox; + + try + { + mbox = new FMessageBox ( caption + , FString().setNumber(num) + , button0, button1, button2 + , parent ); + } + catch (const std::bad_alloc& ex) + { + std::cerr << "not enough memory to alloc " << ex.what() << std::endl; + return FDialog::Reject; + } + reply = mbox->exec(); delete mbox; return reply; @@ -205,9 +227,20 @@ int FMessageBox::error ( FWidget* parent { int reply; const FString& caption = "Error message"; - FMessageBox* mbox = new FMessageBox ( caption, message - , button0, button1, button2 - , parent ); + FMessageBox* mbox; + + try + { + mbox = new FMessageBox ( caption, message + , button0, button1, button2 + , parent ); + } + catch (const std::bad_alloc& ex) + { + std::cerr << "not enough memory to alloc " << ex.what() << std::endl; + return FDialog::Reject; + } + mbox->beep(); mbox->setHeadline("Warning:"); mbox->setCenterText(); @@ -278,29 +311,37 @@ void FMessageBox::init (int button0, int button1, int button2) button_digit[1] = button1; button_digit[2] = button2; - button[0] = new FButton (this); - button[0]->setText(button_text[button0]); - button[0]->setPos(3, getHeight()-4, false); - button[0]->setWidth(1, false); - button[0]->setHeight(1, false); - button[0]->setFocus(); - - if ( button1 > 0 ) + try { - button[1] = new FButton(this); - button[1]->setText(button_text[button1]); - button[1]->setPos(17, getHeight()-4, false); - button[1]->setWidth(0, false); - button[1]->setHeight(1, false); + button[0] = new FButton (this); + button[0]->setText(button_text[button0]); + button[0]->setPos(3, getHeight()-4, false); + button[0]->setWidth(1, false); + button[0]->setHeight(1, false); + button[0]->setFocus(); + + if ( button1 > 0 ) + { + button[1] = new FButton(this); + button[1]->setText(button_text[button1]); + button[1]->setPos(17, getHeight()-4, false); + button[1]->setWidth(0, false); + button[1]->setHeight(1, false); + } + + if ( button2 > 0 ) + { + button[2] = new FButton(this); + button[2]->setText(button_text[button2]); + button[2]->setPos(32, getHeight()-4, false); + button[2]->setWidth(0, false); + button[2]->setHeight(1, false); + } } - - if ( button2 > 0 ) + catch (const std::bad_alloc& ex) { - button[2] = new FButton(this); - button[2]->setText(button_text[button2]); - button[2]->setPos(32, getHeight()-4, false); - button[2]->setWidth(0, false); - button[2]->setHeight(1, false); + std::cerr << "not enough memory to alloc " << ex.what() << std::endl; + return; } resizeButtons(); diff --git a/src/fobject.cpp b/src/fobject.cpp index 1787db2e..473f266e 100644 --- a/src/fobject.cpp +++ b/src/fobject.cpp @@ -27,14 +27,34 @@ FObject::FObject (FObject* parent) if ( parent == 0 ) { - + timer_modify_lock = false; if ( ! timer_list ) - timer_list = new TimerList(); + { + try + { + timer_list = new TimerList(); + } + catch (const std::bad_alloc& ex) + { + std::cerr << "not enough memory to alloc " << ex.what() << std::endl; + return; + } + } if ( ! fc::empty_string ) - fc::empty_string = new FString(""); + { + try + { + fc::empty_string = new FString(""); + } + catch (const std::bad_alloc& ex) + { + std::cerr << "not enough memory to alloc " << ex.what() << std::endl; + return; + } + } } else has_parent = true; @@ -176,7 +196,17 @@ int FObject::addTimer (int interval) timer_modify_lock = true; if ( ! timer_list ) - timer_list = new TimerList(); + { + try + { + timer_list = new TimerList(); + } + catch (const std::bad_alloc& ex) + { + std::cerr << "not enough memory to alloc " << ex.what() << std::endl; + return -1; + } + } // find an unused timer id if ( ! timer_list->empty() ) diff --git a/src/fprogressbar.cpp b/src/fprogressbar.cpp index 5d6ff86c..013c3560 100644 --- a/src/fprogressbar.cpp +++ b/src/fprogressbar.cpp @@ -102,7 +102,16 @@ void FProgressbar::hide() if ( size < 0 ) return; - blank = new char[size+1]; + try + { + blank = new char[size+1]; + } + catch (const std::bad_alloc& ex) + { + std::cerr << "not enough memory to alloc " << ex.what() << std::endl; + return; + } + std::memset(blank, ' ', uLong(size)); blank[size] = '\0'; diff --git a/src/fscrollview.cpp b/src/fscrollview.cpp index fb836a65..712cef40 100644 --- a/src/fscrollview.cpp +++ b/src/fscrollview.cpp @@ -762,15 +762,23 @@ void FScrollView::init (FWidget* parent) setForegroundColor (wc.dialog_fg); setBackgroundColor (wc.dialog_bg); - vbar = new FScrollbar(fc::vertical, this); - vbar->setMinimum(0); - vbar->setValue(0); - vbar->hide(); + try + { + vbar = new FScrollbar(fc::vertical, this); + vbar->setMinimum(0); + vbar->setValue(0); + vbar->hide(); - hbar = new FScrollbar(fc::horizontal, this); - hbar->setMinimum(0); - hbar->setValue(0); - hbar->hide(); + hbar = new FScrollbar(fc::horizontal, this); + hbar->setMinimum(0); + hbar->setValue(0); + hbar->hide(); + } + catch (const std::bad_alloc& ex) + { + std::cerr << "not enough memory to alloc " << ex.what() << std::endl; + return; + } vbar->addCallback ( diff --git a/src/fstatusbar.cpp b/src/fstatusbar.cpp index 04827501..5bbb442a 100644 --- a/src/fstatusbar.cpp +++ b/src/fstatusbar.cpp @@ -184,7 +184,16 @@ void FStatusBar::hide() if ( screenWidth < 0 ) return; - blank = new char[screenWidth+1]; + try + { + blank = new char[screenWidth+1]; + } + catch (const std::bad_alloc& ex) + { + std::cerr << "not enough memory to alloc " << ex.what() << std::endl; + return; + } + std::memset(blank, ' ', uLong(screenWidth)); blank[screenWidth] = '\0'; setPrintPos (1, 1); diff --git a/src/fstring.cpp b/src/fstring.cpp index e01e6336..e4659392 100644 --- a/src/fstring.cpp +++ b/src/fstring.cpp @@ -528,7 +528,16 @@ FString& FString::sprintf (const char* format, ...) if ( len >= int(sizeof(buf)) ) { - buffer = new char[len+1](); + try + { + buffer = new char[len+1](); + } + catch (const std::bad_alloc& ex) + { + std::cerr << bad_alloc_str << ex.what() << std::endl; + return (*this); + } + va_start (args, format); vsnprintf (buffer, uLong(len+1), format, args); va_end (args); @@ -2216,8 +2225,16 @@ inline void FString::initLength (uInt len) length = len; bufsize = FWDBUFFER + len + 1; - string = new wchar_t[bufsize](); - std::wmemset (string, L'\0', bufsize); + + try + { + string = new wchar_t[bufsize](); + std::wmemset (string, L'\0', bufsize); + } + catch (const std::bad_alloc& ex) + { + std::cerr << bad_alloc_str << ex.what() << std::endl; + } } //---------------------------------------------------------------------- diff --git a/src/fterm.cpp b/src/fterm.cpp index 1a415a6a..30bde495 100644 --- a/src/fterm.cpp +++ b/src/fterm.cpp @@ -900,13 +900,24 @@ const FString* FTerm::getXTermFont() { if ( std::scanf("\033]50;%[^\n]s", temp) == 1 ) { + FString* xtermfont; std::size_t n = std::strlen(temp); // BEL + '\0' = string terminator if ( n >= 5 && temp[n-1] == BEL[0] && temp[n] == '\0' ) temp[n-1] = '\0'; - return new FString(temp); + try + { + xtermfont = new FString(temp); + } + catch (const std::bad_alloc& ex) + { + std::cerr << "not enough memory to alloc " << ex.what() << std::endl; + return 0; + } + + return xtermfont; } } } @@ -942,11 +953,24 @@ const FString* FTerm::getXTermTitle() // Esc + \ = OSC string terminator if ( n >= 2 && temp[n-2] == ESC[0] && temp[n-1] == '\\' ) { + FString* xtermtitle; + if ( n < 4 ) return 0; temp[n-2] = '\0'; - return new FString(temp); + + try + { + xtermtitle = new FString(temp); + } + catch (const std::bad_alloc& ex) + { + std::cerr << "not enough memory to alloc " << ex.what() << std::endl; + return 0; + } + + return xtermtitle; } } } @@ -2180,7 +2204,15 @@ int FTerm::getScreenFont() font.height = 32; font.charcount = 512; // initialize with 0 - font.data = new uChar[data_size](); + try + { + font.data = new uChar[data_size](); + } + catch (const std::bad_alloc& ex) + { + std::cerr << "not enough memory to alloc " << ex.what() << std::endl; + return -1; + } // font operation ret = ioctl (fd_tty, KDFONTOP, &font); @@ -2619,9 +2651,17 @@ char* FTerm::parseAnswerbackMsg (char*& current_termtype) char* new_termtype = current_termtype; // send ENQ and read the answerback message - answer_back = new FString(getAnswerbackMsg()); + try + { + answer_back = new FString(getAnswerbackMsg()); + } + catch (const std::bad_alloc& ex) + { + std::cerr << "not enough memory to alloc " << ex.what() << std::endl; + return 0; + } - if ( answer_back && *answer_back == FString("PuTTY") ) + if ( *answer_back == FString("PuTTY") ) { putty_terminal = true; @@ -2662,10 +2702,10 @@ char* FTerm::parseSecDA (char*& current_termtype) // secondary device attributes (SEC_DA) <- decTerminalID string sec_da = new FString(getSecDA()); } - - catch (const std::bad_alloc&) + catch (const std::bad_alloc& ex) { - return new_termtype; + std::cerr << "not enough memory to alloc " << ex.what() << std::endl; + return new_termtype; } if ( sec_da->getLength() > 5 ) @@ -3579,14 +3619,22 @@ void FTerm::init() char* new_termtype = 0; term_initialized = true; init_term_object = this; - fd_tty = -1; - opti_move = new FOptiMove(); - opti_attr = new FOptiAttr(); - term = new FRect(0,0,0,0); - mouse = new FPoint(0,0); + fd_tty = -1; - vt100_alt_char = new std::map; - encoding_set = new std::map; + try + { + opti_move = new FOptiMove(); + opti_attr = new FOptiAttr(); + term = new FRect(0,0,0,0); + mouse = new FPoint(0,0); + vt100_alt_char = new std::map; + encoding_set = new std::map; + } + catch (const std::bad_alloc& ex) + { + std::cerr << "not enough memory to alloc " << ex.what() << std::endl; + std::abort(); + } // Define the encoding set (*encoding_set)["UTF8"] = fc::UTF8; diff --git a/src/ftermbuffer.cpp b/src/ftermbuffer.cpp index 477d12f7..0c7c5cf3 100644 --- a/src/ftermbuffer.cpp +++ b/src/ftermbuffer.cpp @@ -52,7 +52,16 @@ int FTermBuffer::writef (const char* format, ...) if ( len >= int(sizeof(buf)) ) { - buffer = new char[len+1](); + try + { + buffer = new char[len+1](); + } + catch (const std::bad_alloc& ex) + { + std::cerr << "not enough memory to alloc " << ex.what() << std::endl; + return -1; + } + va_start (args, format); vsnprintf (buffer, uLong(len+1), format, args); va_end (args); diff --git a/src/ftextview.cpp b/src/ftextview.cpp index 2e02960f..8cf93c50 100644 --- a/src/ftextview.cpp +++ b/src/ftextview.cpp @@ -150,7 +150,16 @@ void FTextView::hide() if ( size < 0 ) return; - blank = new char[size+1]; + try + { + blank = new char[size+1]; + } + catch (const std::bad_alloc& ex) + { + std::cerr << "not enough memory to alloc " << ex.what() << std::endl; + return; + } + std::memset(blank, ' ', uLong(size)); blank[size] = '\0'; @@ -276,7 +285,16 @@ void FTextView::clear() if ( size < 0 ) return; - blank = new char[size+1]; + try + { + blank = new char[size+1]; + } + catch (const std::bad_alloc& ex) + { + std::cerr << "not enough memory to alloc " << ex.what() << std::endl; + return; + } + std::memset(blank, ' ', uLong(size)); blank[size] = '\0'; @@ -419,9 +437,17 @@ void FTextView::onMouseDown (FMouseEvent* ev) const FPoint& tp = ev->getTermPos(); const FPoint& p = parent->termToWidgetPos(tp); parent->setFocus(); - FMouseEvent* _ev = new FMouseEvent (fc::MouseDown_Event, p, tp, b); - FApplication::sendEvent (parent, _ev); - delete _ev; + + try + { + FMouseEvent* _ev = new FMouseEvent (fc::MouseDown_Event, p, tp, b); + FApplication::sendEvent (parent, _ev); + delete _ev; + } + catch (const std::bad_alloc& ex) + { + std::cerr << "not enough memory to alloc " << ex.what() << std::endl; + } } } @@ -441,9 +467,17 @@ void FTextView::onMouseUp (FMouseEvent* ev) const FPoint& tp = ev->getTermPos(); const FPoint& p = parent->termToWidgetPos(tp); parent->setFocus(); - FMouseEvent* _ev = new FMouseEvent (fc::MouseUp_Event, p, tp, b); - FApplication::sendEvent (parent, _ev); - delete _ev; + + try + { + FMouseEvent* _ev = new FMouseEvent (fc::MouseUp_Event, p, tp, b); + FApplication::sendEvent (parent, _ev); + delete _ev; + } + catch (const std::bad_alloc& ex) + { + std::cerr << "not enough memory to alloc " << ex.what() << std::endl; + } } if ( vbar->isVisible() ) @@ -469,9 +503,17 @@ void FTextView::onMouseMove (FMouseEvent* ev) const FPoint& tp = ev->getTermPos(); const FPoint& p = parent->termToWidgetPos(tp); parent->setFocus(); - FMouseEvent* _ev = new FMouseEvent (fc::MouseMove_Event, p, tp, b); - FApplication::sendEvent (parent, _ev); - delete _ev; + + try + { + FMouseEvent* _ev = new FMouseEvent (fc::MouseMove_Event, p, tp, b); + FApplication::sendEvent (parent, _ev); + delete _ev; + } + catch (const std::bad_alloc& ex) + { + std::cerr << "not enough memory to alloc " << ex.what() << std::endl; + } } } @@ -604,15 +646,23 @@ void FTextView::init() setForegroundColor (wc.dialog_fg); setBackgroundColor (wc.dialog_bg); - vbar = new FScrollbar(fc::vertical, this); - vbar->setMinimum(0); - vbar->setValue(0); - vbar->hide(); + try + { + vbar = new FScrollbar(fc::vertical, this); + vbar->setMinimum(0); + vbar->setValue(0); + vbar->hide(); - hbar = new FScrollbar(fc::horizontal, this); - hbar->setMinimum(0); - hbar->setValue(0); - hbar->hide(); + hbar = new FScrollbar(fc::horizontal, this); + hbar->setMinimum(0); + hbar->setValue(0); + hbar->hide(); + } + catch (const std::bad_alloc& ex) + { + std::cerr << "not enough memory to alloc " << ex.what() << std::endl; + return; + } vbar->addCallback ( diff --git a/src/ftogglebutton.cpp b/src/ftogglebutton.cpp index 08d959c5..12fd4bd8 100644 --- a/src/ftogglebutton.cpp +++ b/src/ftogglebutton.cpp @@ -217,7 +217,16 @@ void FToggleButton::hide() if ( size < 0 ) return; - blank = new char[size+1]; + try + { + blank = new char[size+1]; + } + catch (const std::bad_alloc& ex) + { + std::cerr << "not enough memory to alloc " << ex.what() << std::endl; + return; + } + std::memset(blank, ' ', uLong(size)); blank[size] = '\0'; setPrintPos (1, 1); @@ -485,7 +494,16 @@ void FToggleButton::drawLabel() length = text.getLength(); hotkeypos = -1; - LabelText = new wchar_t[length+1](); + try + { + LabelText = new wchar_t[length+1](); + } + catch (const std::bad_alloc& ex) + { + std::cerr << "not enough memory to alloc " << ex.what() << std::endl; + return; + } + txt = text; src = const_cast(txt.wc_str()); dest = const_cast(LabelText); diff --git a/src/fvterm.cpp b/src/fvterm.cpp index 75bdcc92..c57c77ce 100644 --- a/src/fvterm.cpp +++ b/src/fvterm.cpp @@ -317,7 +317,15 @@ int FVTerm::printf (const char* format, ...) if ( len >= int(sizeof(buf)) ) { - buffer = new char[len+1](); + try + { + buffer = new char[len+1](); + } + catch (const std::bad_alloc& ex) + { + std::cerr << "not enough memory to alloc " << ex.what() << std::endl; + return -1; + } va_start (args, format); vsnprintf (buffer, uLong(len+1), format, args); va_end (args); @@ -903,7 +911,16 @@ void FVTerm::createArea ( int offset_left, int offset_top { // initialize virtual window - area = new term_area; + try + { + area = new term_area; + } + catch (const std::bad_alloc& ex) + { + std::cerr << "not enough memory to alloc " << ex.what() << std::endl; + return; + } + area->widget = static_cast(this); resizeArea (offset_left, offset_top, width, height, rsw, bsh, area); } @@ -966,15 +983,31 @@ void FVTerm::resizeArea ( int offset_left, int offset_top if ( area->text != 0 ) delete[] area->text; - area->changes = new line_changes[height + bsh]; - area->text = new char_data[area_size]; + try + { + area->changes = new line_changes[height + bsh]; + area->text = new char_data[area_size]; + } + catch (const std::bad_alloc& ex) + { + std::cerr << "not enough memory to alloc " << ex.what() << std::endl; + return; + } } else if ( area->width + area->right_shadow != width + rsw ) { if ( area->text != 0 ) delete[] area->text; - area->text = new char_data[area_size]; + try + { + area->text = new char_data[area_size]; + } + catch (const std::bad_alloc& ex) + { + std::cerr << "not enough memory to alloc " << ex.what() << std::endl; + return; + } } else return; @@ -2221,8 +2254,17 @@ void FVTerm::init() init_object = this; vterm = 0; vdesktop = 0; - term_pos = new FPoint(-1,-1); - output_buffer = new std::queue; + + try + { + term_pos = new FPoint(-1,-1); + output_buffer = new std::queue; + } + catch (const std::bad_alloc& ex) + { + std::cerr << "not enough memory to alloc " << ex.what() << std::endl; + std::abort(); + } // Preset to false hidden_cursor = \ diff --git a/src/fwidget.cpp b/src/fwidget.cpp index 16a26c72..759dad7d 100644 --- a/src/fwidget.cpp +++ b/src/fwidget.cpp @@ -2276,10 +2276,19 @@ void FWidget::onClose (FCloseEvent* ev) //---------------------------------------------------------------------- void FWidget::init() { - window_list = new widgetList(); - dialog_list = new widgetList(); - always_on_top_list = new widgetList(); - close_widget = new widgetList(); + try + { + // Initialize widget lists + window_list = new widgetList(); + dialog_list = new widgetList(); + always_on_top_list = new widgetList(); + close_widget = new widgetList(); + } + catch (const std::bad_alloc& ex) + { + std::cerr << "not enough memory to alloc " << ex.what() << std::endl; + return; + } char* cursor_off_str = disableCursor(); @@ -2290,7 +2299,7 @@ void FWidget::init() visible_cursor = ! hideable; - // determine width and height of the terminal + // Determine width and height of the terminal detectTermSize(); wsize.setRect(1, 1, getColumnNumber(), getLineNumber()); adjust_wsize = wsize; @@ -2309,7 +2318,14 @@ void FWidget::init() background_color = wc.term_bg; init_desktop = false; - accelerator_list = new Accelerators(); + try + { + accelerator_list = new Accelerators(); + } + catch (const std::bad_alloc& ex) + { + std::cerr << "not enough memory to alloc " << ex.what() << std::endl; + } } //----------------------------------------------------------------------