{"action":"created","issue":{"url":"https://api.github.com/repos/realm/realm-cocoa/issues/3307","repository_url":"https://api.github.com/repos/realm/realm-cocoa","labels_url":"https://api.github.com/repos/realm/realm-cocoa/issues/3307/labels{/name}","comments_url":"https://api.github.com/repos/realm/realm-cocoa/issues/3307/comments","events_url":"https://api.github.com/repos/realm/realm-cocoa/issues/3307/events","html_url":"https://github.com/realm/realm-cocoa/issues/3307","id":139938757,"number":3307,"title":"EXC_BAD_ACCESS when refreshing the Realm","user":{"login":"Legoless","id":1434530,"avatar_url":"https://avatars.githubusercontent.com/u/1434530?v=3","gravatar_id":"","url":"https://api.github.com/users/Legoless","html_url":"https://github.com/Legoless","followers_url":"https://api.github.com/users/Legoless/followers","following_url":"https://api.github.com/users/Legoless/following{/other_user}","gists_url":"https://api.github.com/users/Legoless/gists{/gist_id}","starred_url":"https://api.github.com/users/Legoless/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Legoless/subscriptions","organizations_url":"https://api.github.com/users/Legoless/orgs","repos_url":"https://api.github.com/users/Legoless/repos","events_url":"https://api.github.com/users/Legoless/events{/privacy}","received_events_url":"https://api.github.com/users/Legoless/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/realm/realm-cocoa/labels/Reproduction%20Required","name":"Reproduction Required","color":"fbca04"},{"url":"https://api.github.com/repos/realm/realm-cocoa/labels/T:Bug-Crash","name":"T:Bug-Crash","color":"e11d21"}],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":23,"created_at":"2016-03-10T16:20:57Z","updated_at":"2016-03-29T18:06:17Z","closed_at":null,"body":"## Goals\r\n\r\nI have a `NSOperation` that should update a relation in the default Realm that the application uses. The update is done with 2 primary keys and target objects. The Realm snapshot needs to be at the latest update, before the operation is executed. Operations are executed on a background `NSOperationQueue` on a single thread (`maxConcurrentOperationCount = 1`), only one operation is allowed to be executed at the same time, so there are no race conditions.\r\n\r\n## Expected Results\r\n\r\nThe error does not occur always, but it is common enough to be easily replicated. A new instance of Realm is created for every operation. Autorefresh is disabled and the Realm is forcefully refreshed with `refresh()` call, to be at the latest update. The code works in general, but crashes at the refresh call occasionally. If refresh is not called Realm will not be at the latest update.\r\n\r\n## Actual Results\r\n\r\nThe code crashes with `EXC_BAD_ACCESS` after `refresh()` call. The stack trace shows that the crash happens somewhere inside `group.hpp` file, at line 1197:\r\n\r\n```\r\nstatic void advance_transact(Group& group, ref_type new_top_ref, size_t new_file_size,\r\n                                 _impl::NoCopyInputStream& in)\r\n    {\r\n        group.advance_transact(new_top_ref, new_file_size, in); // Throws\r\n    }\r\n```\r\n\r\nThe crash usually happens at this line. The stack trace is below:\r\n\r\n```\r\n* thread #5: tid = 0x4521f9, 0x000000010b459037 Realm`realm::LinkListColumn::refresh_accessor_tree(unsigned long, realm::Spec const&) + 311, queue = 'com.MyApp.database :: NSOperation 0x7f9972a51100 (QOS: USER_INITIATED)', stop reason = EXC_BAD_ACCESS (code=1, address=0x20)\r\n  * frame #0: 0x000000010b459037 Realm`realm::LinkListColumn::refresh_accessor_tree(unsigned long, realm::Spec const&) + 311\r\n    frame #1: 0x000000010b4e7388 Realm`realm::Table::refresh_column_accessors(unsigned long) + 632\r\n    frame #2: 0x000000010b4fe15a Realm`realm::Table::refresh_accessor_tree() + 666\r\n    frame #3: 0x000000010b472e48 Realm`realm::Group::advance_transact(unsigned long, unsigned long, realm::_impl::NoCopyInputStream&) + 600\r\n    frame #4: 0x000000010b1e344d Realm`realm::_impl::GroupFriend::advance_transact(group=0x00007f99750dbc00, new_top_ref=252536, new_file_size=458752, in=0x0000700000198cf8) + 45 at group.hpp:1197\r\n    frame #5: 0x000000010b377acc Realm`void realm::SharedGroup::advance_read<(anonymous namespace)::TransactLogValidator>(this=0x00007f99750dbc00, history=0x00007f997253f578, observer=0x00007000001993d0, version=(version = 0, index = 0))::TransactLogValidator*, realm::SharedGroup::VersionID) + 1052 at group_shared.hpp:933\r\n    frame #6: 0x000000010b3776a5 Realm`void realm::_impl::SharedGroupFriend::advance_read<(anonymous namespace)::TransactLogValidator>(sg=0x00007f99750dbc00, hist=0x00007f997253f578, obs=0x00007000001993d0, ver=(version = 0, index = 0))::TransactLogValidator*, realm::SharedGroup::VersionID) + 69 at group_shared.hpp:1093\r\n    frame #7: 0x000000010b377655 Realm`void realm::LangBindHelper::advance_read<(anonymous namespace)::TransactLogValidator>(sg=0x00007f99750dbc00, history=0x00007f997253f578, observer=0x00007000001993d0, version=(version = 0, index = 0))::TransactLogValidator&&, realm::SharedGroup::VersionID) + 69 at lang_bind_helper.hpp:362\r\n    frame #8: 0x000000010b377550 Realm`auto realm::_impl::transaction::advance(this=0x00007000001993b8, args=0x00007000001993d0)::$_0::operator()<(anonymous namespace)::TransactLogValidator&>((anonymous namespace)::TransactLogValidator&&&) const + 80 at transact_log_handler.cpp:437\r\n    frame #9: 0x000000010b37716c Realm`(anonymous namespace)::TransactLogObserver::TransactLogObserver<realm::_impl::transaction::advance(this=0x00007000001993d0, context=0x00007f9972d7bbf0, sg=0x00007f99750dbc00, func=0x00007000001993b8, validate_schema_changes=true)::$_0>(realm::BindingContext*, realm::SharedGroup&, realm::_impl::transaction::advance(realm::SharedGroup&, realm::ClientHistory&, realm::BindingContext*, realm::SharedGroup::VersionID)::$_0&&, bool) + 1132 at transact_log_handler.cpp:222\r\n    frame #10: 0x000000010b3768be Realm`(anonymous namespace)::TransactLogObserver::TransactLogObserver<realm::_impl::transaction::advance(this=0x00007000001993d0, context=0x00007f9972d7bbf0, sg=0x00007f99750dbc00, func=0x00007000001993b8, validate_schema_changes=true)::$_0>(realm::BindingContext*, realm::SharedGroup&, realm::_impl::transaction::advance(realm::SharedGroup&, realm::ClientHistory&, realm::BindingContext*, realm::SharedGroup::VersionID)::$_0&&, bool) + 62 at transact_log_handler.cpp:204\r\n    frame #11: 0x000000010b37686a Realm`realm::_impl::transaction::advance(sg=0x00007f99750dbc00, history=0x00007f997253f530, delegate=0x00007f9972d7bbf0, version=(version = 0, index = 0)) + 106 at transact_log_handler.cpp:436\r\n    frame #12: 0x000000010b370daa Realm`realm::Realm::refresh(this=0x00007f9972d68e40) + 362 at shared_realm.cpp:407\r\n    frame #13: 0x000000010b341bee Realm`-[RLMRealm refresh](self=0x00007f9972d60f40, _cmd=\"refresh\") + 46 at RLMRealm.mm:595\r\n    frame #14: 0x000000010b7fb42d RealmSwift`RealmSwift.Realm.refresh (self=0x00007f99727a6900)() -> Swift.Bool + 45 at Realm.swift:526\r\n    frame #15: 0x000000010a73c070 MyApp`MyApp.MyAppRelationOperation.realm.getter : RealmSwift.Realm(self=0x00007f9972a51100) + 880 at MyAppRelationOperation.swift:24\r\n    frame #16: 0x000000010a73c42c MyApp`MyApp.MyAppRelationOperation.main (self=0x00007f9972a51100)() -> () + 44 at MyAppRelationOperation.swift:43\r\n    frame #17: 0x000000010a73d6f2 MyApp`@objc MyApp.MyAppRelationOperation.main (MyApp.MyAppRelationOperation)() -> () + 34 at MyAppRelationOperation.swift:0\r\n    frame #18: 0x000000010d094f8a Foundation`-[__NSOperationInternal _start:] + 646\r\n    frame #19: 0x000000010d094b9b Foundation`__NSOQSchedule_f + 194\r\n    frame #20: 0x000000010e94649b libdispatch.dylib`_dispatch_client_callout + 8\r\n    frame #21: 0x000000010e92c8ec libdispatch.dylib`_dispatch_queue_drain + 2215\r\n    frame #22: 0x000000010e92be0d libdispatch.dylib`_dispatch_queue_invoke + 601\r\n    frame #23: 0x000000010e92ea56 libdispatch.dylib`_dispatch_root_queue_drain + 1420\r\n    frame #24: 0x000000010e92e4c5 libdispatch.dylib`_dispatch_worker_thread3 + 111\r\n    frame #25: 0x000000010ec7e68f libsystem_pthread.dylib`_pthread_wqthread + 1129\r\n    frame #26: 0x000000010ec7c365 libsystem_pthread.dylib`start_wqthread + 13\r\n```\r\n\r\n## Steps to Reproduce\r\n\r\n1. Create a serial `NSOperationQueue`.\r\n2. Create many operations on the queue that create / update objects.\r\n3. Create many `RelationOperation` operations (sample code provided below).\r\n4. Run the code multiple times until crash happens.\r\n\r\n## Code Sample\r\n\r\nThis is the code for a `NSOperation` that triggers the crash (code was simplified and stripped of irrelevant situations, since the crash happens on the beginning):\r\n\r\n```\r\nimport Foundation\r\nimport RealmSwift\r\n\r\nclass RelationOperation : NSOperation {\r\n    \r\n    // Database reference\r\n    private var realmRef : Realm?\r\n\r\n    var sourceClassName = \"\"\r\n    var sourceKeyValue = \"\"\r\n    var targetClassName = \"\"\r\n    var targetKeyValue = \"\"\r\n    \r\n    var realm : Realm {\r\n        get {\r\n            if realmRef == nil {\r\n                realmRef = try! Realm()\r\n                realmRef!.autorefresh = false\r\n            }\r\n            \r\n            realmRef!.refresh() // This is the line that triggers the crash, the first time Realm() is created.\r\n            \r\n            return realmRef!\r\n        }\r\n        set {\r\n            realmRef = newValue\r\n        }\r\n    }\r\n    override func main () {\r\n        var source = realm.dynamicObjectForPrimaryKey(sourceClassName, key:sourceKeyValue) // This line triggers the getter and causes the crash.\r\n        var target = realm.dynamicObjectForPrimaryKey(targetClassName, key:targetKeyValue)\r\n    }\r\n}\r\n```\r\n\r\n## Version of Realm and Tooling\r\n\r\nI'm using latest RealmSwift version 0.98.3, along with the latest Xcode in the Mac App Store (7.2.1 7C1002). Realm is installed with CocoaPods version 0.39.0 and uses Dynamic Frameworks, because it is a Swift project.\r\n\r\nThanks a lot for help, but I really don't know what the issue is here."},"comment":{"url":"https://api.github.com/repos/realm/realm-cocoa/issues/comments/203030570","html_url":"https://github.com/realm/realm-cocoa/issues/3307#issuecomment-203030570","issue_url":"https://api.github.com/repos/realm/realm-cocoa/issues/3307","id":203030570,"user":{"login":"bmunkholm","id":1215687,"avatar_url":"https://avatars.githubusercontent.com/u/1215687?v=3","gravatar_id":"","url":"https://api.github.com/users/bmunkholm","html_url":"https://github.com/bmunkholm","followers_url":"https://api.github.com/users/bmunkholm/followers","following_url":"https://api.github.com/users/bmunkholm/following{/other_user}","gists_url":"https://api.github.com/users/bmunkholm/gists{/gist_id}","starred_url":"https://api.github.com/users/bmunkholm/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/bmunkholm/subscriptions","organizations_url":"https://api.github.com/users/bmunkholm/orgs","repos_url":"https://api.github.com/users/bmunkholm/repos","events_url":"https://api.github.com/users/bmunkholm/events{/privacy}","received_events_url":"https://api.github.com/users/bmunkholm/received_events","type":"User","site_admin":false},"created_at":"2016-03-29T18:06:17Z","updated_at":"2016-03-29T18:06:17Z","body":"@bdash I assume you mean \"can't\" instead of \"can\" in \"These two actions can happen on different threads.\"?"}}