Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deleting objects from memory

Tags:

c++

My company uses a this piece of code to delete ALL objects from memory.
But because of the catch(...) I wonder what happens if the destructor of that object fails (AV)? Is it ok to catch everything silently? If the destructor failed, don't we want to know about this?

#define DELNULL(p) \
{                  \
if (p)             \
   {               \
   try             \
      {delete p;}  \
   catch (...)     \
      {}           \
   p = NULL;       \
   }               \
}                  \
like image 412
Server Overflow Avatar asked Feb 27 '26 19:02

Server Overflow


1 Answers

Is it ok to catch everything silently?

It is generally not OK to silently catch everything (it's rarely OK to silently catch anything). It would be often useful to know about errors.

In some cases it may be better to not let exceptions propagate. For example, if we are in a function called from C, or if we don't want to terminate, and are in destructor or in a noexcept function. But, it would be better to fall back to some other form of error reporting rather than swallowing them silently.

P.S. if (p) check is redundant and can be safely removed.

like image 65
eerorika Avatar answered Mar 01 '26 09:03

eerorika