Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

java.lang.IllegalStateException: Illegal State: Row/Object is no longer valid to operate on. Was it deleted? #1206

Closed
cotfas opened this issue Jun 11, 2015 · 8 comments
Labels

Comments

@cotfas
Copy link

cotfas commented Jun 11, 2015

Hello,

I`m getting
java.lang.IllegalStateException: Illegal State: Row/Object is no longer valid to operate on. Was it deleted?

Info From Crashlytics
Samsung-SM-G850A
Android 5.0.2

HTC ONE X+
Android 4.2.2

I`m using
buzzboxSDK-0.6.5.jar

Raw error:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.cashdivider.app/com.cashdivider.app.gui.SettingsActivity}: java.lang.IllegalStateException: Illegal State: Row/Object is no longer valid to operate on. Was it deleted?
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2790)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2855)
at android.app.ActivityThread.access$900(ActivityThread.java:181)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1474)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:6117)
at java.lang.reflect.Method.invoke(Method.java)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
Caused by: java.lang.IllegalStateException: Illegal State: Row/Object is no longer valid to operate on. Was it deleted?
at io.realm.internal.Row.nativeIsNullLink(Row.java)
at io.realm.internal.Row.isNullLink(Row.java:156)
at io.realm.BackendRealmProxy.getCurrency(BackendRealmProxy.java:81)
at com.cashdivider.app.gui.fragment.SettingsFragment.init(SettingsFragment.java:249)
at com.cashdivider.app.gui.fragment.SettingsFragment.onCreate(SettingsFragment.java:74)
at android.support.v4.app.Fragment.performCreate(Fragment.java:1766)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:917)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1138)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1120)
at android.support.v4.app.FragmentManagerImpl.dispatchCreate(FragmentManager.java:1924)
at android.support.v4.app.FragmentActivity.onCreate(FragmentActivity.java:268)
at android.support.v7.app.AppCompatActivity.onCreate(AppCompatActivity.java:58)
at com.cashdivider.app.gui.BaseActivity.onCreate(BaseActivity.java:90)
at com.cashdivider.app.gui.SettingsActivity.onCreate(SettingsActivity.java:22)
at android.app.Activity.performCreate(Activity.java:6374)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2743)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2855)
at android.app.ActivityThread.access$900(ActivityThread.java:181)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1474)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:6117)
at java.lang.reflect.Method.invoke(Method.java)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)

@cmelchior
Copy link
Contributor

Hi @Rilley
The most likely reason is as the exception says. That you are holding on to a reference that some other threads deleted. Note that Realm is zero-copy. This means that if you query for the same object on two different threads you are actually holding a reference to the same data. Each RealmObject has a .isValid() method you can use if you want to verify that an object is still usable before accessing it.

If this is not the case. Can you provide some sample code that provoke this error?

@cotfas
Copy link
Author

cotfas commented Jun 12, 2015

Thank you for your response

I will add some code to see

public class NamespaceApp extends Application {
private static Context context;
private Backend backend;
private Realm realm;

@Override
public void onCreate() {
    super.onCreate();
    context = this;
}

private void closeRealmAPI() {
    if (realm != null) {
        realm.close();
        realm = null;
    }
}

private void initRealmAPI(Context context) {
    try {
        realm = Realm.getInstance(context);
    } catch (RealmMigrationNeededException ex) {
        ex.printStackTrace();

        // TODO migration
        Realm.deleteRealmFile(context);

        realm = Realm.getInstance(context);
    }
}

private Realm getRealmAPI() {
    if (realm == null) {
        initRealmAPI(this);
    }
    return realm;
}

private Backend getBackendAPI(){
    if (backend == null || !backend.isValid()) {
        backend = InitBackendData.InitBackendData(this);
    }
    return this.backend;
}

private Context getContextAPI(){
    return context;
}

private void setBackendAPI(Backend backend){
    this.backend = backend;
}

public static Backend getBackend(){
    return ((NamespaceApp )context.getApplicationContext()).getBackendAPI();
}
public static Context getContext(){
    return ((NamespaceApp )context.getApplicationContext()).getContextAPI();
}

public static void setBackend(Backend backend){
    ((NamespaceApp )context.getApplicationContext()).setBackendAPI(backend);
}
public static void closeRealm(){
    ((NamespaceApp )context.getApplicationContext()).closeRealmAPI();
}
public static Realm getRealm(){
    return ((NamespaceApp )context.getApplicationContext()).getRealmAPI();
}

Note, I`ve added "!backend.isValid()"

I`m calling on First activity launches
NamespaceApp .initRealm(this);
NamespaceApp .setBackend(InitBackendData.InitBackendData(this));

I make sure to close the realm connection
@OverRide
public void onDestroy() {
NamespaceApp .closeRealm();
super.onDestroy();
}

I`m using within the realm connection,
objectList = NamespaceApp .getBackend();

And when Im saving data Im using:
NamespaceApp.getRealm().beginTransaction();
NamespaceApp.setBackend(new Backend(....))
NamespaceApp.getRealm().commitTransaction();

The InitBackend class:
public class InitBackendData {
public static Backend InitBackendData(Context context) {
Realm realm = Realm.getInstance(context);

    realm.beginTransaction();
    RealmResults backendRealmResults = realm.allObjects(Backend.class);
    realm.commitTransaction();

  // Other initialisations

    realm.close();
    return backend;
}

}

@kneth kneth removed the Pending label Jun 16, 2015
@cmelchior
Copy link
Contributor

Hi @Rilley
That seems to be fine, and the general idea should work. On thing that might be causing this is orientation changes as that will trigger onDestroy/onCreate without actually removing the Activity, which means that if you hold a reference to a RealmObject it will no longer be valid. Could that be the case?

@cotfas
Copy link
Author

cotfas commented Jun 16, 2015

Hi @cmelchior

Thank you for you response.

Im not sure, Ive tested on HTC one X+ the orientation change and it did not crash.

I`ve already updated to the latest realm lib version, and added the checkup isValid, any way I will keep you posted if the error will appear again. I only saw it once on Crashlytics

@cmelchior
Copy link
Contributor

Thanks, because from the info you have provided it should work. So we need a reproducible case to be able to debug this further. I will close this issue for now, but feel free to reopen it if some new information surfaces or you are able to reproduce it.

@rchincho
Copy link

It happens once in a while. No threads involved. Tough to reproduce as it does not happen in second trial.

java.lang.IllegalStateException: Illegal State: Object is no longer valid to operate on. Was it deleted by another thread?
at io.realm.internal.UncheckedRow.nativeGetLong(UncheckedRow.java)
at io.realm.internal.UncheckedRow.getLong(UncheckedRow.java:129)
at io.realm.EstimateRealmProxy.realmGet$id(EstimateRealmProxy.java:107)
at co.desque.app.workm.model.Estimate.getId(Estimate.java:36)
at co.desque.app.workm.estimates.EstimationDetailFragment.makeInvoice(EstimationDetailFragment.java:423)
at co.desque.app.workm.estimates.EstimationDetailFragment.onOptionsItemSelected(EstimationDetailFragment.java:196)

@shahid650
Copy link

Answer:
I got these errors , and While the solution was simple.
1- java.lang.IllegalStateException: Object is no longer managed by Realm. Has it been deleted?
2- ThrowingException 8, Object is no longer valid to operate on. Was it deleted by another thread?, .
11-19 00:19:02.146 21759-21932/zeroturnaround.org.jrebel4androidgettingstarted E/REALM_JNI: Exception has been throw: Object is no longer valid to operate on. Was it deleted by another thread?

The Bad Code:
if (subServiceId != null && !subServiceId.isEmpty()) {
mSubServiceModelDelete.setDeleted(true);
realm.copyToRealmOrUpdate(mSubServiceModelDelete);
} else {

                mSubServiceModelDelete.deleteFromRealm();
                Log.d("TAG", "Service is Deleted from database.." + mSubServiceModelDelete.getSubservicesSubservicename());
            }

The Good and Error Free Code:
if (subServiceId != null && !subServiceId.isEmpty()) {
mSubServiceModelDelete.setDeleted(true);
realm.copyToRealmOrUpdate(mSubServiceModelDelete);
} else {
Log.d("TAG", "Service is Deleted from database.." + mSubServiceModelDelete.getSubservicesSubservicename());
mSubServiceModelDelete.deleteFromRealm();
}

So that was the Solution For This Error, This Was Error, as i was accessing deleted object after deleting it. LOL
Happy Coding :) :)

@ManiaChamp
Copy link

java.lang.IllegalStateException: Object is no longer valid to operate on. Was it deleted by another thread?

Application performing deletes action when it's in the background , once the application comes to the foreground it crashes the App.
I tried using Refersh the real on "onStart()"method still doesn't work.

Is there any way to handle this ?

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 15, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

6 participants