Questions tagged [exception-safety]

93 questions
9
votes
1 answer

C++: why this simple Scope Guard works?

Every looked at scope guard so far has a guard boolean variable. For example, see this discussion: The simplest and neatest c++11 ScopeGuard But a simple guard works (gcc 4.9, clang 3.6.0): template struct finally_t : public C { …
funny_falcon
  • 427
  • 3
  • 11
8
votes
2 answers

Locking a mutex in a destructor in C++11

I have some code which need to be thread safe and exception safe. The code below is a very simplified version of my problem : #include #include std::mutex mutex; int n=0; class Counter{ public: Counter(){ …
Arnaud
  • 3,765
  • 3
  • 39
  • 69
5
votes
1 answer

Implementing std::vector::push_back strong exception safety

I'm implementing my own vector based on post-2018 San Diego draft (N4791) and have some questions regarding implementing strong exception safety. Here is some code: template void Vector::push_back(const…
user3624760
5
votes
4 answers

C++ exceptions vs. C# exceptions

In an old blog entry titled Cleaner, more elegant, and harder to recognize, the author states: In C++ it's not quite so bad because C++ exceptions are raised only at specific points during execution. In C#, exceptions can be raised at any…
fredoverflow
  • 256,549
  • 94
  • 388
  • 662
4
votes
1 answer

state of std::vector after std::bad_alloc

I'm trying to find a online reference to see the exception safety of several std containers. In the case of std::vector, Does it keep the state previous to the push_back call? I would presume the vector has all its objects still valid (no…
lurscher
  • 25,930
  • 29
  • 122
  • 185
4
votes
2 answers

RAII in Objective-C pattern?

I find myself writing code like this to achieve exception safe code: Container* container = [Container new]; @try { while(someCondition) { ElementType* value = [someObject createObjectFromStorage]; [container add:value]; //…
Jörgen Sigvardsson
  • 4,839
  • 3
  • 28
  • 51
4
votes
3 answers

Is it OK to have a throwing swap member-implementation?

The general guideline when writing classes (using the copy-and-swap idiom) is to provide a non throwing swap member function. (Effective C++, 3rd edition, Item 25 and other resources) However, what if I cannot provide the nothrow guarantee because…
Martin Ba
  • 37,187
  • 33
  • 183
  • 337
4
votes
2 answers

Should I use `std::uncaught_exceptions()` to decide whether to throw an exception from my dtor?

I have a class whose ctor makes a driver call, and whose dtor makes the matching terminating/release driver call. Those calls can fail. The problem is naturally with the dtor. I am naturally aware of the common wisdom of avoiding exceptions in…
einpoklum
  • 118,144
  • 57
  • 340
  • 684
4
votes
4 answers

How do I design a function with a strong exception guarantee?

I have a function which I would like to have the strong exception guarantee: class X { /* Fields and stuff */ void some_function() { vector1.push_back(/*...*/); // May Throw vector2.push_back(/*...*/); // May Throw …
SomeProgrammer
  • 1,134
  • 1
  • 6
  • 12
4
votes
2 answers

Is this item from Effective Modern C++ still up to date?

p.146 of effective modern C++ : void processWidget(std::shared_ptr spw, int priority); void cusDel(Widget *ptr);//a custom deleter This was an unsafe call prior to C++17 : processWidget(std::shared_ptr(new Widget, cusDel),…
user
  • 934
  • 6
  • 17
4
votes
2 answers

How to prevent Buffer overflow / array overflow?

I was recently writing code for a custom serial communication protocol. What I did was, I used a part(8/16 bit) of the receiving data to denote how big the frame size is. Based on this data I expect that no of data to follow. I use Crc to accept or…
seetharaman
  • 141
  • 4
4
votes
2 answers

Is std::list's multi-element inserts strongly exception-safe?

In item 17 of exceptional c++, I find this: First, for all containers, multi-element inserts ("iterator range" inserts) are never strongly exception-safe. but in item 1 of effective STL, I find this: If you need transactional semantics for…
guorongfei
  • 309
  • 2
  • 10
4
votes
1 answer

Exceptionsafety of make_unique: Why is f(new T) exception safe

I have been reading GOTW102, and wonder, why make_unique is more exception safe than the other cases, or in detail why f(new T(...)) is more exception safe than f(new T1(...), new T2(...)). The make_unique implementation from the blog…
ted
  • 4,791
  • 5
  • 38
  • 84
4
votes
1 answer

Exception safety with shared_ptr's constructor

In Effective C++ 3/E, I read this: This is exception unsafe code: class Test { }; void foo(const std::shared_ptr &ptr, int i); int bar(); ... foo(std::shared_ptr(new Test), bar()); Because compiler can implement like this: run new…
ikh
  • 10,119
  • 1
  • 31
  • 70
4
votes
3 answers

How can an implementation guarantee that copy constructor of an iterator is no throw?

Clause 23.2.1.10 of C++11 standard says that "no copy ctor of a returned iterator throws an exception" Does this basically state that is it possible for a copy ctor of an iterator not to throw even a bad_alloc presumably (leaving the case where…
Gmt
  • 569
  • 1
  • 5
  • 19