Why sometimes 'self' isn't available while debugging with lldb?

前端 未结 4 1821
隐瞒了意图╮
隐瞒了意图╮ 2021-01-04 05:44

A lot of time (when it\'s not every time) I got the following error when I try to print objects on lldb. Is there some build/debug configuration to change or is this an erro

4条回答
  •  花落未央
    2021-01-04 06:44

    Because it's optimized away. Let's use an example:

    void f(int self) {
      // Here, "self" is live:Its value is used in the call to NSLog()
      NSLog(@"I am %d",self);
      // Here, "self" is dead: Its value is never used.
      // I could do self=0 or self=self*self and nobody would know.
      // An optimizing compiler will typically optimize it away.
      // It might still be on the stack (in the parameters passed to NSLog())
      // but the compiler shouldn't assume this.
      NSLog(@"Another function call: %d", 1);
      // If "self" was on the stack, it will probably now have been overwritten.
    }
    

    Compilers do a lot of things to make your code faster/smaller; forgetting about variables which are no longer needed is a very common optimization.

提交回复
热议问题