(iOS)

The alloc in primary NSObject is different from the source flow of the custom class alloc, and why the alloc in NSObject is not a source project.

This article is a complement to the previous article, which analyzed the alloc source code, by exploring why NSObject’s alloc approach doesn’t take source engineering.

The alloc of NSObject can’t get into the source code

  • Start by adding an object defined by NSObject to the main function in the compilable source code of OBJC4-781. Add breakpoints to both NSObject and LGPersong

  • inallocAdd a breakpoint to the source implementation of, and temporarily close the breakpoint

  • Run target and the breakpoint breaks atNSObjectPart, openallocThe following occurs when the source code is broken and the execution continues

To explore the according to

Step 1: Explore[NSObject alloc]What walk is which step source code

Now, let’s explore why NSObject’s alloc is in this situation. First of all,

  • Open theDebug --> Debug Workflow --> Always Show DisassemlyTo start assembly debugging
  • Close the breakpoints in the source code, leaving only the breakpoints in Main, and rerun the program, as shown in the following compilationNSObjectDid not goallocSource code, but goobjc_alloc

  • Then turn off assembly debugging and search globallyobjc_allocPut a break point on objc_alloc, close it temporarily,

  • Run it again, debug it, break it, open the breakpoint on objc_alloc, and find that it will enterobjc_allocSource code implementation, this time to viewcls 是 NSObject

Why does NSObject go objc_alloc?

First, let’s look at the difference between NSObject and LGPerson

  • NSObjectIs the iOSThe base classAll custom classes need to inherit from NSObject
  • LGPerson 是inheritanceSince theNSObjectThe class,rewritetheNSObjectIn theallocmethods

And then from the first step of assembly, you can see that both NSObject and LGPerson call objc_alloc, so there are two questions:

  • whyNSObjectcallallocThe method will go toobjc_allocThe source code?
  • whyLGPersonIn thealloc 会Walk two times? namelyCall the alloc, enter the source code, and then alsoWalk to the objc_alloc?

LGPerson alloc goes twice Why?

  • First, you need to debug in the source code, inmainIn theLGPersonI’m going to break it on LGPerson and then onalloc 、 objc_alloc 和 callocSource code with breakpoints, run demo, will break inobjc_alloc(You need to temporarily close all breakpoints in the source code before rerunking)

  • Keep running, findLGPersonFor the first timeallocGo to theobjc_alloc --> callAllocAt the bottom of the methodobjc_msgSendTo send a message to the system

  • Continue to execute the code, and you’ll see that you get toalloc --> callAlloc --> _objc_rootAllocWithZOne, that is,IOS – Underlying principles 02: Alloc & Init & New source code analysisAlloc process in source analysis.

Here is the second time you walk into the call stack in the calloc method

So the reason LGPerson goes twice is because it first needs to look for sel, and the imp relationship, and now it needs to look for alloc’s method number, but why does it find objc_alloc? This needs to ask the system, must be the system at the bottom to do some operations. Please read on

Why does alloc go to objc_alloc in NSObject?

This part needs to be analyzed through the LLVM source code, known as LLVM-Project

Preparation: First, you need a copy of the LLVM source code

  • Search the LLVM source codeobjc_alloc

  • searchshouldUseRuntimeFunctionForCombinedAllocInit, which represents version control

  • searchtryEmitSpecializedAllocInit, the very famous special message sent, also not found hereobjc_alloc

  • Keep trying. Open god’s eyes. ThroughallocString search, and if you can’t find it, you can still passomf_alloc:findtryGenerateSpecializedMessageSendTo attempt to generate a special message to send

And then in this case you can find the logic of calling alloc, calling objc_objc instead, and the key code here isEmitObjCAlloc 

  • Jump toEmitObjCAllocYou can see the definition ofallocThe processing is invokedobjc_alloc

And that’s why alloc in NSObject is going to go to objc_alloc, which is actually part of the system level message processing logic, so the initialization of NSObject is done by the system, so it’s not going to go to the alloc source project

conclusion

So let’s summarize the flow of alloc calls in NSObject and alloc calls in custom classes

NSObject 

Custom class