0

I am using the strict mode in android studio but i am unable to locate the code that's causing the error.The error is caused due to an unclosed object but i have checked all my activities and no such object has been left open.

Error Log

02-10 17:25:21.858 29937-29945/com.user E/StrictMode: A resource was acquired at attached stack trace but never released. See java.io.Closeable for information on avoiding resource leaks.
                                                            java.lang.Throwable: Explicit termination method 'close' not called
                                                             at dalvik.system.CloseGuard.open(CloseGuard.java:180)
                                                             at android.database.CursorWindow.<init>(CursorWindow.java:111)
                                                             at android.database.AbstractWindowedCursor.clearOrCreateWindow(AbstractWindowedCursor.java:198)
                                                             at android.database.sqlite.SQLiteCursor.fillWindow(SQLiteCursor.java:140)
                                                             at android.database.sqlite.SQLiteCursor.getCount(SQLiteCursor.java:134)
                                                             at android.database.AbstractCursor.moveToPosition(AbstractCursor.java:219)
                                                             at android.database.AbstractCursor.moveToFirst(AbstractCursor.java:264)
                                                             at com.google.android.gms.internal.zzatg.zza(Unknown Source)
                                                             at com.google.android.gms.internal.zzatg.zza(Unknown Source)
                                                             at com.google.android.gms.internal.zzatw.zzc(Unknown Source)
                                                             at com.google.android.gms.internal.zzatu.zzb(Unknown Source)
                                                             at com.google.android.gms.internal.zzatu.zza(Unknown Source)
                                                             at com.google.android.gms.internal.zzatu$4.run(Unknown Source)
                                                             at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:423)
                                                             at java.util.concurrent.FutureTask.run(FutureTask.java:237)
                                                             at com.google.android.gms.internal.zzato$zzd.run(Unknown Source)
                                                            02-10 17:25:21.859 29937-29945/com.user E/StrictMode: Finalizing a Cursor that has not been deactivated or closed. database = /data/user/0/com.user/databases/google_app_measurement_local.db, table = null, query = select count(1) from messages
                                                            android.database.sqlite.DatabaseObjectNotClosedException: Application did not close the cursor or database object that was opened here
                                                             at android.database.sqlite.SQLiteCursor.<init>(SQLiteCursor.java:98)
                                                             at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:50)
                                                             at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1316)
                                                             at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1255)
                                                             at com.google.android.gms.internal.zzatg.zza(Unknown Source)
                                                             at com.google.android.gms.internal.zzatg.zza(Unknown Source)
                                                             at com.google.android.gms.internal.zzatw.zzc(Unknown Source)
                                                             at com.google.android.gms.internal.zzatu.zzb(Unknown Source)
                                                             at com.google.android.gms.internal.zzatu.zza(Unknown Source)
                                                             at com.google.android.gms.internal.zzatu$4.run(Unknown Source)
                                                             at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:423)
                                                             at java.util.concurrent.FutureTask.run(FutureTask.java:237)
                                                             at com.google.android.gms.internal.zzato$zzd.run(Unknown Source)

My BaseActivity Class

public class BaseActivity extends AppCompatActivity {

public User_Registration user;
private Realm realmDb;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Log.i("called_base", "base");
    realmDb = Realm.getDefaultInstance();

    user = realmDb.where(User_Registration.class).findFirst();
    if (user == null)
        logout();
}

protected void logout() {
    realmDb.executeTransaction(realm -> {
        RealmResults<User_Registration> all = realm.where(User_Registration.class).findAll();
        Log.i("loggingout", "size" + all.size());
        all.deleteAllFromRealm();
        redirect();
    });
    Log.i("loggingout", "called");
}

private void redirect() {
    final Intent intent = new Intent(getApplicationContext(), LoginActivity.class);
    finishAffinity();
    startActivity(intent);
}

private void closeDb() {
    if (realmDb != null && !realmDb.isClosed())
        realmDb.close();
}

@Override
protected void onDestroy() {
    super.onDestroy();
    Log.i("realm", "onDestroyActivityBase");
    closeDb();
}
}

In my activity the way i fetch data

private void fetchData() {
    userDataRepository dataRepository = new userDataRepository();
    Observable<ArrayList<user>> observable = dataRepository.getuser_List();

    observable.subscribe(users -> {
                Log.i("data", users.size() + "");

                items.addAll(users);
                userAdapter.notifyItemRangeInserted(0, users.size());

            });
    }

The UserDataRepository Class

public class UserDataRepository implements UserDataStore, Lifecycle {

private UserDiskData diskData;

public UserDataRepository() {
    this.diskData = new UserDiskData();
}

@Override
public Observable<ArrayList<User>> getUser_List() {

    if (diskData.Users_Size() > 0) {
        return diskData.getUser_List();
    } else {
        final UserCloudData cloudData = new UserCloudData();

        Observable<ArrayList<User>> observable = cloudData.getUser_List();
        diskData.addAllUser(observable);
        return observable;
    }
}

@Override
public Single<User> getUser(int user_id) {

    if (diskData.isExists(user_id)) {
        return diskData.getUser(user_id);
    } else {
        final UserCloudData cloudData = new UserCloudData();

        Single<User> UserSingle = cloudData.getUser(user_id);
        diskData.addUser(UserSingle);
        return UserSingle;
    }
}

@Override
public void onDestroyCall() {
    diskData.onDestroyCall();
}
}

UserDiskData.class

class UserDiskData implements UserDataStore, Lifecycle {

int Users_Size() {
    final Realm realm = Realm.getDefaultInstance();
    final RealmResults<User> results = realm.where(User.class).findAll();
    Log.i("disk", "pulledData" + results.size());
    realm.close();
    return results.size();
}

void addAllUser(Observable<ArrayList<User>> Users) {
    Log.i("disk", "addAll");
    Users.subscribe(Users1 -> {
        final Realm realm = Realm.getDefaultInstance();
        realm.executeTransaction(realmDb -> realmDb.copyToRealmOrUpdate(Users1));
        realm.close();
    });
}

void addUser(Single<User> User) {
    Log.i("disk", "addSingle");
    User.subscribe(Users1 -> {
        final Realm realm = Realm.getDefaultInstance();
        realm.executeTransaction(realmDb -> realmDb.copyToRealmOrUpdate(Users1));
        realm.close();
    });
}

@Override
public Observable<ArrayList<User>> getUser_List() {

    Log.i("disk", "called");
    final Realm realm = Realm.getDefaultInstance();
    final RealmResults<User> results = realm.where(User.class).findAll();
    ArrayList<User> list = new ArrayList<>();
    for (int i = 0; i < results.size(); i++)
        list.add(results.get(i));
    realm.close();
    Log.i("realmDisk", realm.isClosed() + "");
    return Observable.just(list);
}

boolean isExists(int id) {
    Log.i("disk", "isExists");
    final Realm realm = Realm.getDefaultInstance();
    final User User = realm.where(User.class).equalTo("id", id).findFirst();
    realm.close();
    //return User != null;
    return false;
}

@Override
public Single<User> getUser(int user_id) {
    Log.i("disk", "getSingle");
    final Realm realm = Realm.getDefaultInstance();
    final User User = realm.where(User.class).equalTo("id", user_id).findFirst();
    realm.close();
    return Single.just(User);
}

@Override
public void onDestroyCall() {

}
}

These are the only classes that use realm.

Christian Melchior
  • 19,978
  • 5
  • 62
  • 53
Jude Fernandes
  • 7,437
  • 11
  • 53
  • 90
  • 1
    Except Realm has nothing to do with SQLite or Cursors, so that's an entirely wrong guess. It's more likely to be in some Google Play Services related code, either push notifications or Firebase analytics. – EpicPandaForce Feb 10 '17 at 12:30
  • @EpicPandaForce I have indeed used firebase in my code but its just basic code from the documentation displayed in the firebase documentation.Does this mean i can ignore the errors that strict mode is telling me about? – Jude Fernandes Feb 10 '17 at 12:34
  • 1
    Well I think the strict mode violation comes from firebase internals – EpicPandaForce Feb 10 '17 at 12:35

0 Answers0