(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
- in
alloc
Add a breakpoint to the source implementation of, and temporarily close the breakpoint
- Run target and the breakpoint breaks at
NSObject
Part, openalloc
The 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 the
Debug --> Debug Workflow --> Always Show Disassemly
To 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 compilation
NSObject
Did not goalloc
Source code, but goobjc_alloc
- Then turn off assembly debugging and search globally
objc_alloc
Put 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 enter
objc_alloc
Source code implementation, this time to viewcls
是NSObject
Why does NSObject go objc_alloc?
First, let’s look at the difference between NSObject and LGPerson
NSObject
Is the iOSThe base class
All custom classes need to inherit from NSObjectLGPerson
是inheritance
Since theNSObject
The class,rewrite
theNSObject
In thealloc
methods
And then from the first step of assembly, you can see that both NSObject and LGPerson call objc_alloc, so there are two questions:
- why
NSObject
callalloc
The method will go toobjc_alloc
The source code? - why
LGPerson
In 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, in
main
In theLGPerson
I’m going to break it on LGPerson and then onalloc
、objc_alloc
和calloc
Source code with breakpoints, run demo, will break inobjc_alloc
(You need to temporarily close all breakpoints in the source code before rerunking)
- Keep running, find
LGPerson
For the first timealloc
Go to theobjc_alloc --> callAlloc
At the bottom of the methodobjc_msgSend
To send a message to the system
- Continue to execute the code, and you’ll see that you get to
alloc --> 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 code
objc_alloc
- search
shouldUseRuntimeFunctionForCombinedAllocInit
, which represents version control
- search
tryEmitSpecializedAllocInit
, the very famous special message sent, also not found hereobjc_alloc
- Keep trying. Open god’s eyes. Through
alloc
String search, and if you can’t find it, you can still passomf_alloc:
findtryGenerateSpecializedMessageSend
To 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 to
EmitObjCAlloc
You can see the definition ofalloc
The 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