Don't store arbitrary Context in singletons, which causes memory leak.

We have singletons that store a Context passed to getInstance().
The problem is that when we call them, we casually pass any Context at hand.
If it's an activity (which is often the case), it'll never be GCed.

This CL make them store the application context insteaed.

Change-Id: I1abcc2c08d3f8201416d6c14720f041693823b4e
This commit is contained in:
Makoto Onuki 2010-05-20 16:11:26 -07:00
parent 12e64b09d8
commit 968be441b4
6 changed files with 44 additions and 46 deletions

View File

@ -71,7 +71,7 @@ public class Controller {
private static int MESSAGEID_TO_MAILBOXID_COLUMN_MAILBOXID = 1;
protected Controller(Context _context) {
mContext = _context;
mContext = _context.getApplicationContext();
mProviderContext = _context;
mLegacyController = MessagingController.getInstance(mContext);
mLegacyController.addListener(mLegacyListener);
@ -79,7 +79,6 @@ public class Controller {
/**
* Gets or creates the singleton instance of Controller.
* @param _context The context that will be used for all underlying system access
*/
public synchronized static Controller getInstance(Context _context) {
if (sInstance == null) {

View File

@ -124,7 +124,7 @@ public class MessagingController implements Runnable {
private final Context mContext;
protected MessagingController(Context _context) {
mContext = _context;
mContext = _context.getApplicationContext();
mThread = new Thread(this);
mThread.start();
@ -133,8 +133,6 @@ public class MessagingController implements Runnable {
/**
* Gets or creates the singleton instance of MessagingController. Application is used to
* provide a Context to classes that need it.
* @param application
* @return
*/
public synchronized static MessagingController getInstance(Context _context) {
if (sInstance == null) {

View File

@ -97,7 +97,7 @@ public class SecurityPolicy {
* Private constructor (one time only)
*/
private SecurityPolicy(Context context) {
mContext = context;
mContext = context.getApplicationContext();
mDPM = null;
mAdminName = new ComponentName(context, PolicyAdmin.class);
mAggregatePolicy = null;

View File

@ -98,6 +98,7 @@ public abstract class Sender {
throws MessagingException {
Sender sender = sSenders.get(uri);
if (sender == null) {
context = context.getApplicationContext();
sender = findSender(context, R.xml.senders_product, uri);
if (sender == null) {
sender = findSender(context, R.xml.senders, uri);

View File

@ -167,10 +167,10 @@ public abstract class Store {
* @throws MessagingException
*/
public synchronized static Store getInstance(String uri, Context context,
PersistentDataCallbacks callbacks)
throws MessagingException {
PersistentDataCallbacks callbacks) throws MessagingException {
Store store = sStores.get(uri);
if (store == null) {
context = context.getApplicationContext();
StoreInfo info = StoreInfo.getStoreInfo(uri, context);
if (info != null) {
store = instantiateStore(info.mClassName, uri, context, callbacks);

View File

@ -195,7 +195,7 @@ public class ExchangeStore extends Store {
* Private constructor - use public factory.
*/
private ExchangeTransport(URI uri, Context context) throws MessagingException {
mContext = context;
mContext = context.getApplicationContext();
setUri(uri);
}