目录

NewArray API Not Paired

Call operator delete[] in a timely manner after use to release the array memory allocated via operator new[]. Otherwise, the allocated resources cannot be reclaimed, causing memory leaks.

Rule Configuration

// rules.json

{
  "rules": {
     "cpp": {
       "ResourceLeak": [
         {
           "ruleId": "NewArray API Not Paired",
           "severity": "error",
           "skip": false
         }
       ]
     }
   }
}

Example of Correct Code

// Positive example

class MyClass {
public:
    MyClass() {}
    ~MyClass() {}
};
void NewArrayWithDelete()
{
    MyClass *arr = new MyClass[100];
    
    // Use the array memory.
    
    // Properly release the array memory.
    delete[] arr;
    arr = nullptr;
}

Example of Incorrect Code

// Negative example

class MyClass {
public:
    MyClass() {}
    ~MyClass() {}
};
void NewArrayWithoutDelete() 
{
    // Allocate memory using new[].
    MyClass *arr = new MyClass[100];
    
    // Use the array memory.
    
    // Not released - memory leak!
}

For details about how to configure Code Scanner check rules, see Using codescanner.