Realm-cocoa: Load of null pointer of type 'RLMAccessorContext *' error

Created on 16 Nov 2017  路  15Comments  路  Source: realm/realm-cocoa

I am getting the following errors/warnings in XCode, every time I run the my app:

screen shot 2017-11-16 at 10 52 07

Along with these warnings sometimes my app hangs on startup and I have to restart it.
Any ideas what might potentially cause those issues ?


Realm framework version: Latest

Xcode version: 9.1

iOS/OSX version: 11.1

Dependency manager + version: CocoaPods

T-Help

Most helpful comment

@austinzheng Here is a stack trace for the ubsan report : https://gist.github.com/hhanesand/3e67226caed0a6122e75db991ffe7521

The error message "Reference binding to null pointer of type RLMClassInfo" occurs here. For some reason, both the _realm and the _info pointer are nil :

screen shot 2017-11-27 at 4 04 11 pm

I am willing to share my project if this is of any priority.

All 15 comments

Unfortunately that screenshot isn't enough information for us to understand what sort of problem you're facing. If you could provide us with a reproduction case that allows us to see this issue for ourselves, that would be highly appreciated.

I found the problem. The warning appears because I was looking up for a Realm object by it's primary key (String) and I was providing a nil instead of a proper primary key.... But it looks weird to me that it didn't crash but instead it threw some warnings 馃え

@austinzheng Actually here is what I have:

import RealmSwift

final class Usage: Object {
    @objc dynamic var id = ""
    @objc dynamic var user: User? //<--- Realm object 
    @objc dynamic var startTime: Date!
    @objc dynamic var endTime: Date?
    @objc dynamic var machine: Machine? //<--- Realm object 

    override static func primaryKey() -> String? {
        return #keyPath(Usage.id)
    }

    override static func indexedProperties() -> [String] {
        return [#keyPath(Usage.id)]
    }
}

extension Usage {
    convenience init(usage: UsageResponse) {
        self.init()
        self.id = usage.id

        self.startTime = Date.Formatter.iso8601.date(from: usage.startTime)
        self.endTime = Date.Formatter.iso8601.date(from: usage.endTime)

        guard let realm = try? Realm() else {
            return
        }

        let machineId = usage.machineId
        if let machine = realm.object(ofType: Machine.self, forPrimaryKey: machineId) {
            self.machine = machine
        }

        let userId = usage.user.id
        if let user = realm.object(ofType: User.self, forPrimaryKey: userId) {
            self.user = user
        } else {
            self.user = User(user: usage.user)
        }

    }
}

The problem occurs when I try to assign the machine and user properties. Interesting because even though the warnings are thrown I can see with Realm Browser that the objects are saved correctly 馃. Can you see anything wrong with the code aboce @austinzheng ?

Just saw this issue as well. Will report if I find anything useful.

@austinzheng Here is a stack trace for the ubsan report : https://gist.github.com/hhanesand/3e67226caed0a6122e75db991ffe7521

The error message "Reference binding to null pointer of type RLMClassInfo" occurs here. For some reason, both the _realm and the _info pointer are nil :

screen shot 2017-11-27 at 4 04 11 pm

I am willing to share my project if this is of any priority.

If you could send us a project (or even a cut down repro case), and repro instructions to [email protected], that would be immensely appreciated!

Just to follow up on this issue. I work with @kristiyandobrev on a project where this problem occurs. It only seem to happen on the iOS simulator not on a real device if that is to any help.

I've seen it happen on device.

@austinzheng

I get an UBSan warning in RLMGetObject() on both Simulator and real device with RealmSwift 3.0.2 and 2.10.2. Seems same/related to this issue.

Thread 1: Null pointer use
Reference binding to null pointer of type 'RLMAccessorContext'

This is the line:

 auto obj = realm::Object::get_for_primary_key(*c, realm->_realm, *info.objectSchema,
                                                      key ?: NSNull.null);

If we check the full function we see that c is assigned nullptr and later dereferenced when calling get_for_primary_key().

id RLMGetObject(RLMRealm *realm, NSString *objectClassName, id key) {
    RLMVerifyRealmRead(realm);

    RLMAccessorContext *c = nullptr;
    auto& info = realm->_info[objectClassName];
    if (RLMProperty *prop = info.propertyForPrimaryKey()) {
        RLMValidateValueForProperty(key, info.rlmObjectSchema, prop);
    }
    try {
---->        auto obj = realm::Object::get_for_primary_key(*c, realm->_realm, *info.objectSchema,
                                                      key ?: NSNull.null);
        if (!obj.is_valid())
            return nil;
        return RLMCreateObjectAccessor(realm, info, obj.row());
    }
    catch (std::exception const& e) {
        @throw RLMException(e);
    }
}

Figured out what was causing this on my end. I was accessing a RLMLinkingObjects that was created by a unmanaged realm object, which caused the _info pointer to be nil. Doesn't seem like this is an error on your end @austinzheng, but maybe Realm can handle it a little better.

I see methods such as lastObject and firstObject on RLMResults that return early if the _info pointer is nil. An early return in valueForKey: that checks if the _info pointer is nil would avoid the warning in my case. Thoughts?

@nevil @Fogh @kristiyandobrev Could you all post the full stack traces so we can figure out if we're seeing similar problems?

I can't comment in any official capacity any longer, but if a crash occurs in Realm-related code at runtime and there isn't an associated RLMException (or, at worst, C++ exception), then it's certainly a bug and needs to be addressed.

@tgoyne
Do you have any comment on what I wrote earlier?
https://github.com/realm/realm-cocoa/issues/5478#issuecomment-353907644

UBSan is warning because of dereferencing a null ptr:

RLMAccessorContext *c = nullptr;
...
auto obj = realm::Object::get_for_primary_key(*c, realm->_realm, *info.objectSchema,
                                                      key ?: NSNull.null);

Seeing these errors in runtime on device:

Pods/Realm/Realm/RLMObjectStore.mm:238:55: runtime error: reference binding to null pointer of type 'RLMAccessorContext'
Pods/Realm/include/object_accessor.hpp:309:47: runtime error: reference binding to null pointer of type 'RLMAccessorContext'
Pods/Realm/include/RLMAccessor.hpp:83:10: runtime error: load of null pointer of type 'RLMAccessorContext *'
Pods/Realm/Realm/RLMAccessor.mm:687:39: runtime error: load of null pointer of type 'RLMAccessorContext *'

and here is a print of some vars when app hits a breakpoint in RLMGetObject(RLMRealm *realm, NSString *objectClassName, id key)

(lldb) p info
(RLMClassInfo) $0 = {
  realm = 0x00000001c04bd700
  rlmObjectSchema = 0x00000001c40bb5a0
  objectSchema = 0x00000001129cee20
  observedObjects = size=0 {}
  m_table = 0x0000000000000000
  m_linkTargets = size=0 {}
}

(lldb) po key
26fc9999-b72a-4a7d-b589-13942612620e

(lldb) po info.propertyForPrimaryKey()
guid {
    type = string;
    objectClassName = (null);
    linkOriginPropertyName = (null);
    indexed = YES;
    isPrimary = YES;
    array = NO;
    optional = YES;
}

(lldb) p *info.objectSchema
(const realm::ObjectSchema) $5 = {
  name = "Note"
  persisted_properties = size=13 {
    [0] = {
      name = "identifier"
      type = Int
      object_type = ""
      link_origin_property_name = ""
      is_primary = (m_value = false)
      is_indexed = (m_value = true)
      table_column = 1
    }
    [1] = {
      name = "guid"
      type = 66
      object_type = ""
      link_origin_property_name = ""
      is_primary = (m_value = true)
      is_indexed = (m_value = true)
      table_column = 0
    }
    [2] = {
      name = "modified"
      type = 68
      object_type = ""
      link_origin_property_name = ""
      is_primary = (m_value = false)
      is_indexed = (m_value = false)
      table_column = 2
    }
    ... <some other columns here>
  }
  computed_properties = size=0 {}
  primary_key = "guid"
}

_(this is in Realm 3.1.1)_

@uson1x
Isn't your log is the same as mine which is is caused by RLMAccessorContext *c = nullptr; in RLMGetObject()?

@nevil yeah, you are right, forgot to check the context pointer. It is 0x0000000000000000 indeed.

Was this page helpful?
0 / 5 - 0 ratings