Uploaded image for project: 'Realm Cocoa SDK'
  1. Realm Cocoa SDK
  2. RCOCOA-694

[realm-core-6.0.6] Assertion failed: has_refs()

    • Type: Icon: Task Task
    • Resolution: Fixed
    • Priority: Icon: Critical - P2 Critical - P2
    • None
    • Affects Version/s: None
    • Component/s: None

      Goals

      On a background thread, I want to be able to use "Wrapper Objects" that use equality and hash based on an RLMObject primaryKey

      Expected Results

      I should be able to wrap any RLMObject with a primaryKey into an object that overrides hash and equality and insert them into a set to perform union and other set operations.

      Actual Results

      The app crashes very often when trying to access the primary key of an RLMObject that is wrapped

      Steps to Reproduce

      /Users/realm/workspace/realm_realm-core_release_6.0.6/src/realm/array.hpp:1136: [realm-core-6.0.6] Assertion failed: has_refs()
      0   Realm                               0x000000010c955d6c _ZN5realm4utilL18terminate_internalERNSt3__118basic_stringstreamIcNS1_11char_traitsIcEENS1_9allocatorIcEEEE + 28
      1   Realm                               0x000000010c956008 _ZN5realm4util9terminateEPKcS2_lOSt16initializer_listINS0_9PrintableEE + 324
      2   Realm                               0x000000010c8740a4 _ZN5realm7Cluster4initENS_6MemRefE + 156
      3   Realm                               0x000000010c873984 _ZNK5realm16ClusterNodeInner7try_getENS_6ObjKeyERNS_11ClusterNode5StateE + 204
      4   Realm                               0x000000010c87cbec _ZNK5realm11ClusterTree8is_validENS_6ObjKeyE + 36
      5   Realm                               0x000000010c8de60c _ZNK5realm8ConstObj8is_validEv + 100
      6   Realm                               0x000000010c6793c8 ___ZN12_GLOBAL__N_115makeBoxedGetterIN5realm10StringDataEEEP11objc_objectm_block_invoke + 48
      7   MyApp                           0x000000010172bcf8 $sSo21TRGC09transportbC2IdSSSgvpABTK + 64
      8   MyApp                           0x000000010172bd90 $sSo21TRGC09transportbC2IdSSSgvpABTK + 216
      9   libswiftCore.dylib                  0x00000001ba8195a0 01A8EE3D-C8DC-396B-89AD-A5F64DF13F80 + 955808
      10  libswiftCore.dylib                  0x00000001ba81912c 01A8EE3D-C8DC-396B-89AD-A5F64DF13F80 + 954668
      11  libswiftCore.dylib                  0x00000001ba818e48 01A8EE3D-C8DC-396B-89AD-A5F64DF13F80 + 953928
      12  libswiftCore.dylib                  0x00000001ba81b824 swift_getAtKeyPath + 176
      13  MyApp                           0x0000000101af5f1c $s6RMyApp13ObjectWrapperC2eeoiySbACyxq_G_AEtFZ + 284
      14  MyApp                           0x0000000101af645c $s6MyApp13ObjectWrapperCyxq_GSQAASQ2eeoiySbx_xtFZTW + 24
      15  libswiftCore.dylib                  0x00000001ba85f22c $sSh8_VariantV6removeyxSgxF + 428
      16  MyApp                           0x0000000101728f28 $sSo27TRGC6RabbitE23enumerateExistingValues33_5102C99FD4F65A5C1106ABE8612FF5B8LL4from2in7keyPath5blockySayxG_AJs03KeyT0Cyxq_Gyx_xSitXEtSHR_r0_lFZ + 1824
      17  MyApp                           0x000000010172bc80 $sSo27TRGC6MyApptE21enumerateExistingTRGs4from2in5blockySaySo0abC0CG_AJyAI_AISitXEtFZ + 320
      18  MyApp                           0x000000010172bfb8 $sSo27TRGC6MyAppE21enumerateExistingTRGs4from2in5blockySaySo0abC0CG_AJyAI_AISitXEtFZTo + 21239  libdispatch.dylib                   0x000000010ce4de98 _dispatch_block_async_invoke2 + 104
      40  libdispatch.dylib                   0x000000010ce3f730 _dispatch_client_callout + 16
      41  libdispatch.dylib                   0x000000010ce42390 _dispatch_continuation_pop + 524
      42  libdispatch.dylib                   0x000000010ce41810 _dispatch_async_redirect_invoke + 680
      43  libdispatch.dylib                   0x000000010ce50d74 _dispatch_root_queue_drain + 376
      44  libdispatch.dylib                   0x000000010ce51698 _dispatch_worker_thread2 + 152
      45  libsystem_pthread.dylib             0x00000001acfd0b38 _pthread_wqthread + 212
      46  libsystem_pthread.dylib             0x00000001acfd3740 start_wqthread + 8!!! IMPORTANT: Please send this log and info about Realm SDK version and other relevant reproduction info to help@realm.io.
      

      Code Sample

      /// The wrapper object that wrapps RLMobjects so they can be inserted into Sets and perform set operations like containsObject, union...
      public class ObjectWrapper<T,U>: Hashable, Equatable where U: Hashable & Equatable {
          public let wrappedObject: T
          private let primaryKeyPath: KeyPath<T, U>
          public init(with object: T, primaryKeyPath: KeyPath<T, U>) {
              self.wrappedObject = object
              self.primaryKeyPath = primaryKeyPath
          }
          
          public static func == (lhs: ObjectWrapper<T, U>, rhs: ObjectWrapper<T, U>) -> Bool {
              return lhs.wrappedObject[keyPath: lhs.primaryKeyPath] == rhs.wrappedObject[keyPath: lhs.primaryKeyPath]
          }
          
          public func hash(into hasher: inout Hasher) {
              hasher.combine(wrappedObject[keyPath: primaryKeyPath])
          }
      }
      
      /// A method to enumerate existing values from one Array in the other Array
      private static func enumerateExistingValues<T, U:Hashable & Equatable>(from lhs:Array<T>, in rhs:Array<T>, keyPath: KeyPath<T, U>, block: (T, T, Int) -> Void) {
              var leftSet: Set<ObjectWrapper<T, U>> = []
              var rightSet:Set<ObjectWrapper<T, U>> = []
              for value in lhs {
                  leftSet.insert(ObjectWrapper<T, U>(with: value, primaryKeyPath: keyPath))
              }
              for value in rhs {
                  rightSet.insert(ObjectWrapper<T, U>(with: value, primaryKeyPath: keyPath))
              }
              
              for (index, leftValue) in leftSet.enumerated() {
                  if let rightValue = rightSet.remove(leftValue) {
                      block(leftValue.wrappedObject, rightValue.wrappedObject, index)
                  }
              }
          }
      

      Line triggering the crash:

          public static func == (lhs: ObjectWrapper<T, U>, rhs: ObjectWrapper<T, U>) -> Bool {
              return lhs.wrappedObject[keyPath: lhs.primaryKeyPath] == rhs.wrappedObject[keyPath: lhs.primaryKeyPath]
          }
      

      Version of Realm and Tooling

      Realm framework version: 5.0.3

      Realm Object Server version: N/A

      Xcode version: 11.3

      iOS/OSX version: 13.5.1

      Dependency manager + version: N/A

            Assignee:
            finn.schiermer-andersen@mongodb.com Finn Andersen (Inactive)
            Reporter:
            unitosyncbot Unito Sync Bot
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: