Android Content Provider database leak issue

android, java

Solution

You're not missing anything AFAIK. Android is missing an `onDestroy()` (or the equivalent) for `ContentProvider`. There isn't even anything in the source code in this area that suggests there is some sort of `onDestroy()` that just isn't surfaced in the SDK.

If you look at the source code for `AlarmProvider` and `LauncherProvider`, they even create database objects on a per-API-call basis (e.g., every time they get `insert()`, they open a writable database handle that they never close).

Problem

I am writing a content provider for this application and in my content provider I am opening a database connection, running a query and returning the cursor of results to the calling program. If I close this database connection in the provider, the cursor has no results. If I leave it open, I get "leak found" errors in my DDMS log. What am I missing here? What's the clean, proper way to return a cursor of database results?

Original source