V1006. Several shared_ptr objects are initialized by the same pointer. A double memory deallocation will occur.


The analyzer has detected an error that has to do with several objects of type 'shared_ptr' being initialized by the same pointer. This will result in undefined behavior when the second object of type 'shared_ptr' tries to free the storage already freed by the first object.

Consider the following example of incorrect code:

void func()
{
  S *rawPtr = new S(10, 20);
  std::shared_ptr<S> shared1(rawPtr);
  std::shared_ptr<S> shared2(rawPtr);
  ....
}

When the function returns, the 'shared1' object will delete the 'rawPtr' pointer, and then the 'shared2' object will try to delete it once again, causing undefined behavior of the program.

Fixed code:

void func()
{
  std::shared_ptr<S> shared1(new S(10, 20));
  std::shared_ptr<S> shared2(new S(10, 20));
  ....
}

According to Common Weakness Enumeration, potential errors found by using this diagnostic are classified as CWE-675.


Bugs Found

Checked Projects
336
Collected Errors
12 743