RLK (Resource Leak) issues are reported when resources are allocated but not properly disposed after use. ANDROID.RLK.SQLCON indicates that a SQL connection is not closed on exit.

Vulnerability and risk

Resources such as streams, connections and graphic objects must be explicitly closed. The close operation can unblock transactions or flush file changes in the file system. While a resource will eventually be closed by the garbage collector, resource exhaustion can occur before garbage collection starts. Depending on the nature of the resource, various exceptions will be thrown on a failed attempt to allocate another resource, for example: Too many open files or too many database connections.

Mitigation and prevention

Explicitly close all resources that have the close method, even those that you think are not doing anything significant. Future code changes will then be safe from such errors.

Example 1

     protected void onCreate(Bundle bundle) {
         final SQLiteDatabase database = openOrCreateDatabase(DB_NAME, Context.MODE_PRIVATE, null);
         final Cursor query = database.query(DATABASE_TABLE,
                                             new String[]{KEY_FILE, KEY_DATE, KEY_COMMENT},
                                             KEY_DATE + " asc");
         ListAdapter adapter = new SimpleCursorAdapter(this,
                                                       new String[]{Contacts.People.NAME},
                                                       new int[]{});

ANDROID.RLK.SQLCON is reported for the snippet on line 32: connection 'database' is not closed on exit.

Related checkers


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