Summary of basic principles of iOS

This article complements the previous article, which analyzed the source code for alloc, by exploring why NSObject’s alloc methods don’t go through the source project. Alloc in NSObject is the difference between the source flow of alloc in NSObject and that of custom class Alloc, and why alloc in NSObject doesn’t go through the source project.

NSObject alloc cannot enter source code

Start by adding an object defined by NSObject to the main function in the compiled source code. NSObject and LGPerson also add breakpoints.

You run target, you break at NSObject, you open the breakpoint of the alloc source, and you continue, and you see that the breakpoint is not broken, and LGPerson is broken.

To explore the according to

Step 1: Explore[NSObject alloc]Which step is the source code

Now, let’s explore why NSObject alloc does this, first of all,

  • Open theDebug --> Debug Workflow --> Check Always Show DisassemlyTo enable assembly debugging

Close the breakpoint of the source code, leaving only the breakpoint in main, rerunce the program, and by looking at the assembly below you can see that NSObject does not go to alloc source code, but to objc_alloc

Then turn off assembly debugging, search objC_alloc globally, and add a breakpoint to objC_alloc.

Why NSObject goes to objc_alloc?

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

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

Then, from the assembly shown in step 1, 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?

Why the alloc goes twice in LGPerson?

  • First, you need to debug in the source codemainIn theLGPersonPut a breakpoint on LGPerson and then onalloc 、 objc_alloc 和 callocSource code plus breakpoints, run will break atobjc_allocThe source code.

If you continue running, you’ll find that LGPerson’s first alloc goes to objc_alloc –> callAlloc at the bottom of the objc_msgSend method to send a message to the system

If you continue to execute the code, you will find that you will go to alloc –> callAlloc –> _objc_rootAllocWithZOne, which is the alloc flow for source analysis of alloc & init & new.

Here is the call stack for the second walk into a calloc method

Therefore, it can be concluded from the above debugging process that the reason why LGPerson goes twice is that it first needs to find SEL and the relation of imp. Currently, it needs to find alloc method, but why objc_Alloc is found? You have to ask the system, it must be doing something at the bottom.

NSObject alloc goes to objc_alloc why?

This part needs to be analyzed through the LLVM source code (llVM-project)

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

Through omf_alloc: find tryGenerateSpecializedMessageSend, said trying to generate a special message

Then in this case you can find the logic that calls alloc, which in turn calls objc_objc, and the key code is EmitObjCAlloc

Jump to the definition of EmitObjCAlloc and you can see that alloc is handled by calling objc_alloc

It follows that the alloc in NSObject goes to objc_alloc, which is actually part of the system-level message processing logic, so the initialization of NSObject is done by the system, and therefore does not go to the source engineering of alloc.

conclusion

To summarize the alloc call flow in NSObject and custom classes

NSObject

Custom class