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++

Manual resource management is error-prone

The first and most obvious danger of managing every resource manually, with explicit calls to acquire and release each one, is that it is easy to forget the latter. For example see the following:

{    object_counter* p = new object_counter;      ... many more lines of code ... } // Were we supposed to do something here? Can't remember now...

We are now leaking a resource (object_counter objects, in this case). If we did this in a unit test, it would fail, as follows:

TEST(Memory, Leak1) {    object_counter::all_count = object_counter::count = 0;    object_counter* p = new object_counter;    EXPECT_EQ(1, object_counter::count);    EXPECT_EQ(1, object_counter::all_count);    //delete p;    // Forgot that EXPECT_EQ(0, object_counter::count); ...
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