为什么 alloc 方法没有显示在调用堆栈中?

Why is the alloc method not shown in the call stack?

我正在尝试了解内存堆栈。我 运行 我的项目并检查堆栈如何变化,同时在项目中移动 "step in"。

我介入了这样的事情:

myClass *tmp = [[myClass alloc] init];

然后我看到:

0-[myClass init]
1-[AppDelegate application:didFinishLaunchingWithOptions:]
...

为什么0和1之间没有[myClass alloc]? 我想这是因为 alloc 的实施已关闭?

myClass* tmp = [[myClass alloc] init];

相当于:

myClass* x = [myClass alloc];
myClass* tmp = [x init];

因此 initalloc 的结果进行操作。换句话说,当 init 发生时 alloc 必须已经完成并返回。

如果 alloc 调用了 init,那么您可以预期它会出现在堆栈跟踪中。这就是调用堆栈的工作原理。