COP-alloc-ctor (C++ only)
Synopsis
A class member is deallocated in the class' destructor, but not allocated in a constructor or assignment operator.
Enabled by default
No
Severity/Certainty
High/Medium

Full description
A class member is deallocated in the class' destructor but is not allocated in a constructor or assignment operator (operator=). Even if this is intentional (and the class' pointer attributes are allocated elsewhere) it is still dangerous, because it subverts the Resource Acquisition is Initialization convention, and consequently users of the class might accidentally misuse it.
Coding standards
- CWE 401
Improper Release of Memory Before Removing Last Reference ('Memory Leak')
Code examples
The following code example fails the check and will give a warning:
class MyClass{
int *p;
public:
MyClass(){} //p is not allocated in
//this constructor
~MyClass(){
delete p;
}
};
The following code example passes the check and will not give a warning about this issue:
class MyClass{
int *p;
public:
MyClass(){
p = new int(0); //OK - p is allocated
}
~MyClass(){
delete p;
}
};