【问题标题】:Obj C: Unable to access singleton from debuggerObj C:无法从调试器访问单例
【发布时间】:2015-03-16 12:55:54
【问题描述】:

所以这很奇怪。我刚刚在我的应用程序中创建了一个新的单例,使用的模式与我用于许多其他单例的模式相同。然而,这个在调试器中表现不佳。下面是获取单例的代码:

- (id)init
{
    self = [super init];
    if (self) {
        [self loadData];
    }
    return self;
}

+ (Settings *)sharedInstance
{
    static Settings *sharedInstance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedInstance = [[self alloc] init];
        // Do any other initialisation stuff here
    });
    return sharedInstance;
}

现在当我尝试从调试器访问对象时,它给了我这个:

error: Execution was interrupted, reason: EXC_BAD_ACCESS (code=1, address=0x20).
The process has been returned to the state before expression evaluation.

显然,我不只是试图从调试器中获取单例的句柄,而是试图获取一个属性。这给出了一些更有趣的输出:

(lldb) po [Settings sharedInstance].jpegCompressionLevel
error: warning: got name from symbols: Settings
warning: receiver type 'void *' is not 'id' or interface pointer, consider casting it to 'id'
error: no known method '-sharedInstance'; cast the message send to the method's return type
error: 1 errors parsing expression

这里到底发生了什么?代码中的调用似乎很好。调试器不断失败。在相同的上下文中,我可以访问其他单例(使用相同的模式)就好了。

可能值得注意的是,loadData 方法只是从磁盘加载字典,并且类属性的 getter 使用该字典中的值,而不是 ivars。

这是 loadData 的代码:

-(void)loadData
{
    // Load data from persistent storage
    NSString *path = [self pathToDataFile];
    NSMutableDictionary *settingsDict = [NSMutableDictionary dictionaryWithDictionary:[NSKeyedUnarchiver unarchiveObjectWithFile:path]];
    _settingsDict = settingsDict;
}

【问题讨论】:

  • 如您所见,lldb 正在寻找-sharedInstance,这是一个实例方法而不是类方法。看看this 是否有帮助。

标签: ios objective-c debugging singleton singleton-methods


【解决方案1】:

现在我无法解释原因,但将课程从“设置”重命名为其他名称可以解决问题。我在 Symbol Navigator 中看不到任何与这个名字冲突的东西……确实很奇怪。

【讨论】:

  • 完全独立于类名。对于AVMediaTypeAudio,我遇到了同样的错误。它是一个系统常量,似乎无法在调试器中使用它
猜你喜欢
  • 1970-01-01
  • 2016-06-17
  • 1970-01-01
  • 2020-11-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多