mardi 29 octobre 2019

Modern C++ makes memory leaks harder to find

How are you supposed to test modern C++ for memory leaks?

Out unit test cases typically look like this:

TestCase {
  Instantiate testObject
  testObject->AllocateSomeResources
  testObject->PerformATest
  testObject->DeallocateResources
  Destroy testObject
}

We use valgrind for detecting memory leaks. This is very effective if the resource allocation is done with new and delete, but when the resources are stored as smart pointers inside standard containers, the automatic cleanup at testObject destruction prevents us from finding faults.

When the system is live. resource containers may grow over time due to a faulty deallocation procedure. This would have been trivial to find if the allocation was done with new and delete.

Are there any techniques that will remedy this aspect of modern C++?

Ideas:

  • Make all destructors test for allocated resources. This would impact the production code base quite a bit.
  • Create more separated and testable allocation and deallocation procedures. Of course, but since we got alot of this "for free" with more traditional C++, i still would like to find alternatives.

Obviously i see a benefit in standard containers and smart pointers, sorry about the click bait header

Aucun commentaire:

Enregistrer un commentaire