Start here

Home
About Klocwork
What's new
Fixed issues
Release notes
Installation

Reference

C/C++ checkers
Java checkers
C# checkers
MISRA C 2004 checkers
MISRA C++ 2008 checkers
MISRA C 2012 checkers
MISRA C 2012 checkers with Amendment 1
Commands
Metrics
Troubleshooting
Reference

Product components

C/C++ Integration build analysis
Java Integration build analysis
Desktop analysis
Refactoring
Klocwork Static Code Analysis
Klocwork Code Review
Structure101
Tuning
Custom checkers

Coding environments

Visual Studio
Eclipse for C/C++
Eclipse for Java
IntelliJ IDEA
Other

Administration

Project configuration
Build configuration
Administration
Analysis performance
Server performance
Security/permissions
Licensing
Klocwork Static Code Analysis Web API
Klocwork Code Review Web API

Community

View help online
Visit RogueWave.com
Klocwork Support
Rogue Wave Videos

Legal

Legal information

CL.MLK.ASSIGN

Memory leak in an assignment operator

This is a class-level (CL) checker that notifies of potential for a memory leak in operator=. Klocwork reports CL.MLK.ASSIGN when a class performing dynamic memory allocation in the constructor overwrites the corresponding pointers in operator=, without the prior memory release and/or decrementing the proper reference counter. This can lead to a memory leak.

Vulnerability and risk

Memory leaks cause the application to consume additional memory. This reduces the amount of memory available to other applications and eventually causes the operating system to start paging, slowing the system down. In critical cases, the application will reach overall memory limits, which may result in application crashes.

Vulnerable code example 1

1    class C {
2    public:
3        C() { ip = new int; }
4        ~C() { delete ip; }
5        C& operator=(const C& rhs) {
6            if (this == &rhs) return *this;
7            ip = new int;      // memory pointed by ip is leaked
8            *ip = *rhs.ip;
9            return *this;
10        }
11   private:
12       C(const C&);
13       int* ip;
14   };

In this example, memory pointed by ip is not released before being overwritten. Since memory is allocated into this pointer in the constructor, this assignment leads to a possible memory leak that is reported by Klocwork as CL.MLK.ASSIGN.

Fixed code example 1

1    class C {
2    public:
3        C() { ip = new int; }
4        ~C() { delete ip; }
5        C& operator=(const C& rhs) {
6            if (this == &rhs) return *this;
7            delete ip;
8            ip = new int;
9            *ip = *rhs.ip;
10           return *this;
11       }
12   private:
13       C(const C&);
14       int* ip;
15   };

In the fixed example 1, the dynamic memory pointed by ip is released at line 7 prior to the assignment to a new value.

Vulnerable code example 2

1    class counted {
2    public:
3        counted() { counter = 1; }
4        void addRef() { counter++; }
5        void decRef() { counter--; if (counter == 0) delete this; }
6        /* other methods */
7    private:
8        int counter;
9        /* other members */
10   };
11
12   class C {
13   public:
14       C() { cp = new counted; }
15       ~C() { cp->decRef(); }
16       C& operator=(const C& rhs) {
17           if (this == &rhs) return *this;
18           cp = rhs.cp;    // CL.MLK.ASSIGN reported
19           cp->addRef();
20           return *this;
21       }
22   private:
23       C(const C&);
24       counted* cp;
25   };

In this example, the reference counter of memory pointed by cp is not decremented before the overwrite that can lead to memory leaks and is reported by Klocwork as CL.MLK.ASSIGN.

Fixed code example 2

1    class counted {
2    public:
3        counted() { counter = 1; }
4        void addRef() { counter++; }
5        void decRef() { counter--; if (counter == 0) delete this; }
6        /* other methods */
7    private:
8        int counter;
9        /* other members */
10   };
11
12   class C {
13   public:
14       C() { cp = new counted(); }
15       ~C() { cp->decRef(); }
16       C& operator=(const C& rhs) {
17           if (this == &rhs) return *this;
18           cp->decRef();
19           cp = rhs.cp;
20           cp->addRef();
21           return *this;
22       }
23   private:
24       C(const C&);
25       counted* cp;
26   };          

Now, the reference counter is decremented, and no defect is reported.

Extension

This checker can be extended through the Klocwork knowledge base. See Tuning C/C++ analysis for more information.