CWE-772: Missing Release of Resource after Effective Lifetime

Description:
The product does not release a resource after its effective lifetime has ended, i.e., after the resource is no longer needed.
Affected technologies: Mobile
Parent weaknesses: CWE-404, CWE-404, CWE-404, CWE-404

Consequences:
  - Availability: DoS: Resource Consumption (Other)

Detection methods:
  - Automated Static Analysis: Automated static analysis, commonly referred to as Static Application Security Testing (SAST), can find some instances of this weakness by analyzing source code (or binary/compiled code) without havin

Mitigations:
  - [Requirements] Use a language that does not allow this weakness to occur or provides constructs that make this weakness easier to avoid. 
                   For example, languages such as Java, Ruby, and Lisp perform automatic garbage collection that releases memory for objects that have been deallocated.
  - [Implementation] It is good practice to be responsible for freeing all resources you allocate and to be consistent with how and where you free resources in a function. If you allocate resources that you intend to free upon completion of the function, you must be sure to free the resources at all exit points for that
  - [Operation] Use resource-limiting settings provided by the operating system or environment. For example, when managing system resources in POSIX, setrlimit() can be used to set limits for certain types of resources, and getrlimit() can determine how many resources are available. However, these functions are not

Demonstrative examples:
The following method never closes the new file handle. Given enough time, the Finalize() method for BufferReader should eventually call Close(), but there is no guarantee as to how long this action will take. In fact, there is no guarantee that Finalize() will ever be invoked. In a busy environment, the Operating System could use up all of the available file handles before the Close() function is called.
The good code example simply adds an explicit call to the Close() function when the system is done using the file. Within a simple example such as this the problem is easy to see and fix. In a real system, the problem may be considerably more obscure.
private void processFile(string fName) { BufferReader fil = new BufferReader(new FileReader(fName)); String line; while ((line = fil.ReadLin

The following code attempts to open a new connection to a database, process the results returned by the database, and close the allocated SqlConnection object.
The problem with the above code is that if an exception occurs while executing the SQL or processing the results, the SqlConnection object is not closed. If this happens often enough, the database will run out of available cursors and not be able to execute any more SQL queries.
SqlConnection conn = new SqlConnection(connString); SqlCommand cmd = new SqlCommand(queryString); cmd.Connection = conn; conn.Open(); SqlDataReader rdr = cmd.ExecuteReader(); HarvestResults(rdr); conn.Connection.Close();

Real-world examples:
  - CVE-2007-0897: Chain: anti-virus product encounters a malformed file but returns from a function without closing a file descriptor (CWE-775) leading to file descriptor consumption (CWE-400) and failed scans.
  - CVE-2001-0830: Sockets not properly closed when attacker repeatedly connects and disconnects from server.
  - CVE-1999-1127: Does not shut down named pipe connections if malformed data is sent.
  - CVE-2009-2858: Chain: memory leak (CWE-404) leads to resource exhaustion.
  - CVE-2009-2054: Product allows exhaustion of file descriptors when processing a large number of TCP packets.