exception-safety

Are deferred functions called when calling log.Fatalln?

孤街浪徒 提交于 2019-12-29 07:30:50
问题 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? 回答1: No, the deferred functions aren't run. Here's the description of log.Fatal : Fatal is equivalent to Print() followed by a call to os.Exit(1). log.Fatal calls os.Exit , whose description is here : Exit causes the current program to exit with the given

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

假装没事ソ 提交于 2019-12-29 03:14:30
问题 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 cannot be made exception-safe because of its underlying design, that almost always is a signal of its poor design. Example 1: A function with two different responsibilities is difficult to make exception-safe. Example 2: A copy assignment operator

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

↘锁芯ラ 提交于 2019-12-29 03:14:08
问题 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 cannot be made exception-safe because of its underlying design, that almost always is a signal of its poor design. Example 1: A function with two different responsibilities is difficult to make exception-safe. Example 2: A copy assignment operator

Exception safety and make_unique

大兔子大兔子 提交于 2019-12-28 04:13:04
问题 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 f(T*, T*); f(new T, new T); is not, correct? 回答1: Not only when you have multiple allocations, but whenever you can throw at different places. Consider this: f(make_unique<T>(), function_that_can_throw()); Versus: f(unique_ptr<T>(new T), function_that

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

我们两清 提交于 2019-12-20 16:29:10
问题 In cppref, the following holds until C++17: code such as f(std::shared_ptr<int>(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<int>(42), g()) is safe, since two function calls are never interleaved. I'm wondering which change introduced in C++17 renders this no longer applicable. 回答1: The evaluation order of function arguments are changed by P0400R0. Before the change, evaluation of function arguments are

How do I run a cleanup code on the function exit?

戏子无情 提交于 2019-12-20 04:22:54
问题 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<int> vector(1000); // Do some work } But if you have (for some reasons) to use some pure C API, you have either to create C++ wrappers around it or to use try/catch blocks void function() { int *arr = (int*)malloc(1000*sizeof(int)); if (!arr) { throw "cannot malloc"; } try { // Do some work } catch (...) { free(arr); //

state of std::vector after std::bad_alloc

北城以北 提交于 2019-12-12 11:52:44
问题 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 destructors invoked). What guarantees offer std::vector after push_back throws a std::bad_alloc exception? 回答1: If it throws, the vector isn't changed. Even not the capacity() . According to [container.requirements.general]: Unless otherwise specified (see

Cleanly and optionally redirect stderr or stdout to file

女生的网名这么多〃 提交于 2019-12-12 04:59:40
问题 I have a Python3 script and I want to optionally redirect stdout and stderr to a file. Something like this: # variable declarations if log_output: output_file = open('output.txt', 'w') sys.stdout = output_file if log_errors: errors_file = open('errors.txt', 'w') sys.stderr = errors_file # code that uses variables declared above but may exit suddenly #at the end if log_output: output_file.close() if log_errors: errors_file.close() This works, unless my code in the middle decides to quit. Then

std::terminate and destructors of empty containers

大兔子大兔子 提交于 2019-12-12 03:26:00
问题 Consider some standard container which uses dynamic memory (i.e. is an AllocatorAwareContainer) and has a size and capacity of zero. For example, take a std::vector and call vec.resize(0); vec.shrink_to_fit(); . I would imagine that such container instances would contain only nullptr pointers for their logical contents and std::size_t members to track information like size . I would also imagine that their destructors would do essentially nothing, as there is no dynamic memory to be freed.

Exception safety in memory arena

假如想象 提交于 2019-12-11 13:10:01
问题 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 time, and then delete them all when the pool is destroyed. { MemoryArena m; std::string* ptr = m.Allocate<std::string>(); // use ptr whatever // Cleaned up when pool is destroyed } But this gets rather nasty when it's used multiple times. If the