Questions tagged [exception-safety]
93 questions
2
votes
3 answers
How do I run a cleanup code on the function exit?
C++ classes provide RAII idiom. Therefore you don't have to care about exceptions:
void function()
{
// The memory will be freed automatically on function exit
std::vector vector(1000);
// Do some work
}
But if you have…

anton_rh
- 8,226
- 7
- 45
- 73
2
votes
1 answer
raii architecture using c++ and glfw
I'm currently attempting to write a small engine in C++ using glfw for window creation. I want to make strong use of raii to come up with an exception-safe architecture and make the engine practically impossible to use in the wrongway.
But i'm…

cubber
- 23
- 3
2
votes
1 answer
InitOnceExecuteOnce exception safety
I have a exception safety issue on InitOnceExecuteOnce WinAPI function. Whenever exception is thrown from callback function deadlock is occurred. Callback returns boolean flag telling the caller whether the data is successfully initialized, but if I…

axe
- 217
- 3
- 9
2
votes
0 answers
Exception safety guarantee and std::move()
During some implementation I stuck with the problem about exception safety guarantee and using std::move(). I know that SO is not a good place to ask "what is your opinion" (class template Boo) kind of a question but here I would like to make sure…

Artur Pyszczuk
- 1,920
- 1
- 16
- 23
2
votes
2 answers
What is the exception safety guarantee for getter (by value) methods?
For the following example class, what are the exception safety guarantees for the getter methods?
Do such getter methods offer a minimum of a strong guarantee?
Does returning a fundamental type by value always offer a no throw guarantee?
class…

Class Skeleton
- 2,913
- 6
- 31
- 51
2
votes
1 answer
Initialize std::array with make_shared
Background
I am writing a driver for a networkprotocol and have a function write(std::shared_ptr package), where package is std::array (0=>header, 1=> body). For convenience I want to write a function write(buffer_ptr body),…

ted
- 4,791
- 5
- 38
- 84
2
votes
2 answers
Failure handling with destructors vs. catch (...) { fix(); throw; }
Let's say I'm doing something that requires cleanup when an exception is thrown.
For example, say I'm creating a dynamic array, and I need to construct objects, but their constructors may throw an exception:
size_t const n = 100;
T *const p =…

user541686
- 205,094
- 128
- 528
- 886
2
votes
2 answers
Should I write the end of the file in the destructor?
I have some code which looks a bit like this:
void writeToStream( std::ostream & outputStream )
{
MyXmlWriter xmlWriter{ outputStream };
xmlWriter.addNode();
xmlWriter.addNode();
xmlWriter.close(); // should this be called in…

Ralph Tandetzky
- 22,780
- 11
- 73
- 120
2
votes
1 answer
Exception safety C++ shared pointer
I try to implement a JSON framework in C++ and want to make use of polymorphic concepts. I have a class JSONNode which is kind of container that stores other JSONNode objects itself and so on. I am doing this with pointers and dynamic allocation.…

Michbeckable
- 1,851
- 1
- 28
- 41
2
votes
3 answers
Create a XML-File in an exception safe and memory friendly way?
I want to write a Logger/ Bug-Tracker with XML output for my current project. I'm sorry if it should be a duplicate, but the proposals were not useful and I did not found a good google solution too.
1: My first question is about exception safety.
If…

Alex
- 1,857
- 3
- 36
- 51
2
votes
2 answers
Can Java exception safety be made less ugly?
Given the following code: We need to lock the model, then start a transaction (which could throw an exception so we have to make sure the lock is released), then do something kind of like getting a database connection (which could throw an…

Mark Lutton
- 6,959
- 7
- 41
- 57
1
vote
2 answers
Exception safety in memory arena
I'm writing a simple memory arena allocator and facing a small problem with exception safety. The situation is when you allocate an object which itself calls the allocator. The objective of the memory pool is to allocate a bunch of objects at one…

Puppy
- 144,682
- 38
- 256
- 465
1
vote
6 answers
Is synchronized keyword exception-safe?
Possible Duplicate:
Side effects of throwing an exception inside a synchronized clause?
I am wondering if synchronized is exception-safe? Say, an uncaught exception happens within the synchronized block, will the lock be released?

user705414
- 20,472
- 39
- 112
- 155
1
vote
6 answers
Ensure that an object is not null
How can I make sure that a certain instance of a class will never be null? Someone told me to use Debug.Assert() but by doing so, I would only ensure that the code works in debug mode, whereas I want to ensure the is-never-null condition in release…

Amc_rtty
- 3,662
- 11
- 48
- 73
1
vote
0 answers
Copy-and-swap alternatives for the objects which are expensive to copy?
I have read this, this, this and other similar topics. They all show examples of how we can roll back simple changes made to an object in case of an exception to preserve the original state of the object. Yes, if we just push a new element to a…

Alexey104
- 969
- 1
- 5
- 17