From 76c2d3cd5425e3d157f8a1e95ad23183a0f368c5 Mon Sep 17 00:00:00 2001 From: Ari Lazier Date: Thu, 5 May 2016 17:10:56 -0700 Subject: [PATCH] fix leaking realm --- src/js_realm.hpp | 21 ++++++++++++++++--- .../src/impl/apple/weak_realm_notifier.cpp | 2 +- src/object-store/src/shared_realm.cpp | 1 + 3 files changed, 20 insertions(+), 4 deletions(-) diff --git a/src/js_realm.hpp b/src/js_realm.hpp index 8dcc2b73..588acc93 100644 --- a/src/js_realm.hpp +++ b/src/js_realm.hpp @@ -297,11 +297,26 @@ void Realm::constructor(ContextType ctx, ObjectType this_object, size_t argc, if (!Value::is_undefined(ctx, migration_value)) { FunctionType migration_function = Value::validated_to_function(ctx, migration_value, "migration"); config.migration_function = [=](SharedRealm old_realm, SharedRealm realm) { + auto old_realm_ptr = new SharedRealm(old_realm); + auto realm_ptr = new SharedRealm(realm); ValueType arguments[2] = { - create_object>(ctx, new SharedRealm(old_realm)), - create_object>(ctx, new SharedRealm(realm)) + create_object>(ctx, old_realm_ptr), + create_object>(ctx, realm_ptr) }; - Function::call(ctx, migration_function, 2, arguments); + + try { + Function::call(ctx, migration_function, 2, arguments); + } + catch (...) { + old_realm->close(); + old_realm_ptr->reset(); + realm_ptr->reset(); + throw; + } + + old_realm->close(); + old_realm_ptr->reset(); + realm_ptr->reset(); }; } diff --git a/src/object-store/src/impl/apple/weak_realm_notifier.cpp b/src/object-store/src/impl/apple/weak_realm_notifier.cpp index da403ad7..0ce6300f 100644 --- a/src/object-store/src/impl/apple/weak_realm_notifier.cpp +++ b/src/object-store/src/impl/apple/weak_realm_notifier.cpp @@ -34,7 +34,7 @@ WeakRealmNotifier::WeakRealmNotifier(const std::shared_ptr& realm, bool c }; CFRunLoopSourceContext ctx{}; - ctx.info = new RefCountedWeakPointer{realm, {1}}; + ctx.info = new RefCountedWeakPointer{realm, {0}}; ctx.perform = [](void* info) { if (auto realm = static_cast(info)->realm.lock()) { realm->notify(); diff --git a/src/object-store/src/shared_realm.cpp b/src/object-store/src/shared_realm.cpp index 865cbe6b..bdc2b1fd 100644 --- a/src/object-store/src/shared_realm.cpp +++ b/src/object-store/src/shared_realm.cpp @@ -247,6 +247,7 @@ void Realm::update_schema(std::unique_ptr schema, uint64_t version) if (m_config.migration_function) { m_config.migration_function(old_realm, shared_from_this()); } + m_config.migration_function = nullptr; }; try {