From c1e7ddcda1c005142f0b2c8cbc941db582f98c81 Mon Sep 17 00:00:00 2001 From: Kristian Dupont Date: Tue, 20 Dec 2016 16:28:02 +0100 Subject: [PATCH] Throw when deleting detached object (#694) * Throw when deleting detached object * Update error message * Add user file * Throw when deleting detached object * Update error message * Revert "Add user file" This reverts commit 2948f4cfc2dfd2d5d75594307b1e89806b817eb7. --- src/js_realm.hpp | 4 ++++ tests/js/realm-tests.js | 6 ++++++ 2 files changed, 10 insertions(+) diff --git a/src/js_realm.hpp b/src/js_realm.hpp index 68652738..6ff5ae5c 100644 --- a/src/js_realm.hpp +++ b/src/js_realm.hpp @@ -565,6 +565,10 @@ void RealmClass::delete_one(ContextType ctx, ObjectType this_object, size_t a if (Object::template is_instance>(ctx, arg)) { auto object = get_internal>(arg); + if (!object->is_valid()) { + throw std::runtime_error("Object is invalid. Either it has been previously deleted or the Realm it belongs to has been closed."); + } + realm::TableRef table = ObjectStore::table_for_object_type(realm->read_group(), object->get_object_schema().name); table->move_last_over(object->row().get_index()); } diff --git a/tests/js/realm-tests.js b/tests/js/realm-tests.js index cd537f1e..551f55c8 100644 --- a/tests/js/realm-tests.js +++ b/tests/js/realm-tests.js @@ -595,6 +595,12 @@ module.exports = { realm.delete(threeObjects); TestCase.assertEqual(objects.length, 4, 'wrong object count'); TestCase.assertEqual(threeObjects.length, 0, 'threeObject should have been deleted'); + + var o = objects[0]; + realm.delete(o); + TestCase.assertThrows(function() { + realm.delete(o); + }); }); },