Questions tagged [exception-safety]

93 questions
49
votes
4 answers

What is wrong with "checking for self-assignment" and what does it mean?

In Herb Sutter's book Exceptional C++ (1999), he has words in item 10's solution: "Exception-unsafe" and "poor design" go hand in hand. If a piece of code isn't exception-safe, that's generally okay and can simply be fixed. But if a piece of code…
Jimm Chen
  • 3,411
  • 3
  • 35
  • 59
48
votes
14 answers

The simplest and neatest c++11 ScopeGuard

I'm attempting to write a simple ScopeGuard based on Alexandrescu concepts but with c++11 idioms. namespace RAII { template< typename Lambda > class ScopeGuard { mutable bool committed; Lambda rollbackLambda; …
lurscher
  • 25,930
  • 29
  • 122
  • 185
31
votes
7 answers

Ensuring that Exceptions are always caught

Exceptions in C++ don't need to be caught (no compile time errors) by the calling function. So it's up to the developer's judgment whether to catch them using try/catch (unlike in Java). Is there a way one can ensure that the exceptions thrown are…
sachin
  • 1,141
  • 2
  • 16
  • 21
28
votes
2 answers

std::make_shared() change in C++17

In cppref, the following holds until C++17: code such as f(std::shared_ptr(new int(42)), g()) can cause a memory leak if g gets called after new int(42) and throws an exception, while f(std::make_shared(42), g()) is safe, since two…
Lingxi
  • 14,579
  • 2
  • 37
  • 93
27
votes
2 answers

Safety of std::unordered_map::merge()

While writing some code targeting C++17, I kind of hit a stumbling block determining the exception-safety of an operation merging two compatible std::unordered_maps. Per the current working draft, §26.2.7, table 91 reads, in part, regarding the…
Capital C
  • 271
  • 2
  • 5
27
votes
3 answers

Where can I find all the exception guarantees for the Standard Containers and Algorithms?

Yes, I've looked at the C++ standards that I could find (or the drafts), but I'm not finding any comprehensive of the exception guarantees given by STL containers. All I can find are occasional sections with incomplete descriptions on some of the…
user541686
  • 205,094
  • 128
  • 528
  • 886
22
votes
2 answers

Are deferred functions called when calling log.Fatalln?

db, err := sql.Open("postgres", "…") if err != nil { log.Fatalln(err) } defer db.Close() tpl, err := template.ParseGlob("") if err != nil { log.Fatalln(err) } If template.ParseGlob("") returns an error, is db.Close() still being called?
user142019
22
votes
1 answer

Is it safe to use emplace_back with a container of unique_ptrs?

Consider the following: std::vector> ptrsToInts; ptrsToInts.emplace_back(new int); If reallocation occurs in the vector, and that fails (throwing std::bad_alloc), am I "safe" or will I leak an int? C++11 23.3.6.5…
Billy ONeal
  • 104,103
  • 58
  • 317
  • 552
20
votes
4 answers

Is there a C++ standard class to set a variable to a value at scope exit

Within the scope of a member function, I want to temporarly set a member variable to a certain value. Then, when this function returns, I want to reset this member variable to a given known value. To bo safe against exceptions and multiple returns,…
Didier Trosset
  • 36,376
  • 13
  • 83
  • 122
20
votes
3 answers

Exception safety and make_unique

Just to clarify, using make_unique only adds exception safety when you have multiple allocations in an expression, not just one, correct? For example void f(T*); f(new T); is perfectly exception safe (as far as allocations and stuff), while void…
Kal
  • 1,309
  • 11
  • 16
18
votes
1 answer

Exception-safety of C++ implicitly generated assignment operator

My understanding is that C++ implicitly generated assignment operator does a member-wise copy (this seems confirmed also by this answer). But, if during a member copy an exception is thrown (e.g. because a resource for that member can't be…
Mr.C64
  • 41,637
  • 14
  • 86
  • 162
12
votes
2 answers

Sink arguments and move semantics for functions that can fail (strong exception safety)

I have a function that operates on a big chunk of data passed in as a sink argument. My BigData type is already C++11-aware and comes with fully functional move constructor and move assignment implementations, so I can get away without having to…
ComicSansMS
  • 51,484
  • 14
  • 155
  • 166
10
votes
1 answer

Memory leak during unordered_map::insert KeyEqual exception with GCC - breaking the strong exception safety guarantee?

I'm using GCC 7.3.1, but also tested on coliru, which I believe is version 9.2.0. Build with the following: g++ -fsanitize=address -fno-omit-frame-pointer rai.cpp Here's rai.cpp: #include #include int main() { try …
Rai
  • 1,328
  • 11
  • 20
10
votes
1 answer

How exception-safe is std::tie?

std::tie returns a tuple of references, so you can do the following: int foo, bar, baz; std::tie(foo, bar, baz) = std::make_tuple(1, 2, 3); This is similar to foo, bar, baz = (1, 2, 3) in Python. What is supposed to happen if one of the assignments…
user142019
10
votes
3 answers

Exception safety regarding swap() operation - what's so wrong with that?

I keep reading that swap() operation, like this for example: template void swap (T &a, T &b) { T temp (a); a = b; b = temp; } is problematic when we are dealing with Exception-safety. What's so wrong with it? Furthermore, how can…
JAN
  • 21,236
  • 66
  • 181
  • 318
1
2 3 4 5 6 7