fix leaking realm

This commit is contained in:
Ari Lazier 2016-05-05 17:10:56 -07:00
parent 7a6141e066
commit 76c2d3cd54
3 changed files with 20 additions and 4 deletions

View File

@ -297,11 +297,26 @@ void Realm<T>::constructor(ContextType ctx, ObjectType this_object, size_t argc,
if (!Value::is_undefined(ctx, migration_value)) { if (!Value::is_undefined(ctx, migration_value)) {
FunctionType migration_function = Value::validated_to_function(ctx, migration_value, "migration"); FunctionType migration_function = Value::validated_to_function(ctx, migration_value, "migration");
config.migration_function = [=](SharedRealm old_realm, SharedRealm realm) { 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] = { ValueType arguments[2] = {
create_object<T, RealmClass<T>>(ctx, new SharedRealm(old_realm)), create_object<T, RealmClass<T>>(ctx, old_realm_ptr),
create_object<T, RealmClass<T>>(ctx, new SharedRealm(realm)) create_object<T, RealmClass<T>>(ctx, realm_ptr)
}; };
Function<T>::call(ctx, migration_function, 2, arguments);
try {
Function<T>::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();
}; };
} }

View File

@ -34,7 +34,7 @@ WeakRealmNotifier::WeakRealmNotifier(const std::shared_ptr<Realm>& realm, bool c
}; };
CFRunLoopSourceContext ctx{}; CFRunLoopSourceContext ctx{};
ctx.info = new RefCountedWeakPointer{realm, {1}}; ctx.info = new RefCountedWeakPointer{realm, {0}};
ctx.perform = [](void* info) { ctx.perform = [](void* info) {
if (auto realm = static_cast<RefCountedWeakPointer*>(info)->realm.lock()) { if (auto realm = static_cast<RefCountedWeakPointer*>(info)->realm.lock()) {
realm->notify(); realm->notify();

View File

@ -247,6 +247,7 @@ void Realm::update_schema(std::unique_ptr<Schema> schema, uint64_t version)
if (m_config.migration_function) { if (m_config.migration_function) {
m_config.migration_function(old_realm, shared_from_this()); m_config.migration_function(old_realm, shared_from_this());
} }
m_config.migration_function = nullptr;
}; };
try { try {