Skip to Content
Hands-On Design Patterns with C++
book

Hands-On Design Patterns with C++

by Fedor G. Pikus
January 2019
Intermediate to advanced
512 pages
14h 5m
English
Packt Publishing
Content preview from Hands-On Design Patterns with C++

Resource management and exception safety

Remember the code at the beginning of the previous section—the one we said is correct, where we did not forget to release the resource? Consider the following code:

bool process(... some parameters ... ) {    object_counter* p = new object_counter;      ... many more lines of code ...    delete p;    return true;     // Success }

I have bad news for you—this code probably wasn't correct either. If any of the many more lines of code can throw an exception, then delete p is never going to be executed:

bool process(... some parameters ... ) {    object_counter* p = new object_counter;      ... many more lines of code ...    if (!success) // Exceptional circumstances, cannot continue        throw process_exception();  ... even more lines ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Structural Design Patterns in Modern C++

Structural Design Patterns in Modern C++

Umar Lone

Publisher Resources

ISBN: 9781788832564Supplemental Content